• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

randomly picking numbers

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i'm stuck in a situation, i want to pick 10 random elements at a time from a vector.what i have done so far is, i generate a random number uses the generated random number as the index of the element be picked the element from the vector. Now my problem i don't want to pick the element which has already been picked and i know it does that because the indexes being generated repeats due to the random number i'm generating. To get view what i'm really talking about i'm posting a code. Please how do i avoid element already selected. Thanks

[ July 28, 2006: Message edited by: Henry Addo ]
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this an exercise in using random numbers or can you use other methods.
Collections.shuffle() may be worth looking at.
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i would shuffle the List/Vector then pick the 10 (or any other number) elements starting at index 0 (of course you need to have at least 10 elements in the List then).
Look at java.util.Collections for shuffling and at the List.subList() method for creating a sub list (picking the first 10 elements)

This might not be the most performant solution but you can write it in two or three lines.

if you want to do this without shuffling then you need to create "unique" random numbers:



this might be inefficient to (when the number of items you want to pick is close to the number of total items in the list the "algorithm" is not so smart :-) )

pascal
 
Henry Addo
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no is not an assignment. Random number generation was what came to mind the first time i thought about how to solve it. I will be trying all what has been said and will post code when i'm stack. Thanks
 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
why don't you use the java.util.Random? its easy to use...
watch here the javadoc :
http://www.docjar.com/docs/api/java/util/Random.html
arno
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arno, i think that's what he did. He created random numbers (and each number would be the index to an element in a List) but he does not want the same index more than once therefore you need to remember what numbers were generated before (or he will pick the same element from the List more than once).
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hope this helps!!!
 
Henry Addo
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i realised where the problem was lying. i found out that when the button is clicked to call the "pickNumber" function, a new instance of the array to be passed to the "pickNumber" function is created so the element that i remove from the vector does goes away but a new but the same element are added to vector. To solve this, i created a new function that accepts a vector, then a randomly element is remove from that vector. in the actionPerform method i set a counter, when the counter is zero it should call the pickNumber other wise it should call the second function i created which accepts the vector created in the pickNumber funtion. To really explain the english, here is the code


This is not smart enough for me if you think there is a better way of doing this please advice

[ July 28, 2006: Message edited by: Henry Addo ]

[ July 28, 2006: Message edited by: Henry Addo ]
[ July 28, 2006: Message edited by: Henry Addo ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic