File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes randomly picking numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "randomly picking numbers" Watch "randomly picking numbers" New topic
Author

randomly picking numbers

Henry Addo
Greenhorn

Joined: Feb 26, 2005
Posts: 24
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 ]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3539
    
  15
Is this an exercise in using random numbers or can you use other methods.
Collections.shuffle() may be worth looking at.


Joanne
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
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

Joined: Feb 26, 2005
Posts: 24
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
Arno Reper
Ranch Hand

Joined: Mar 14, 2006
Posts: 286
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


The man who makes no mistakes does not usually make anything<br /> <br />>>> SCJP 5.0 >> SCJD B&S <<< In progress
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
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).
Christian Nash
Ranch Hand

Joined: Jan 17, 2006
Posts: 107
Hi Hope this helps!!!


- Christian
Henry Addo
Greenhorn

Joined: Feb 26, 2005
Posts: 24
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: randomly picking numbers