Random number, in a set, generated exclusively

Ok, I don't have a problem generating the random numbers in a set. I am working on generating random for a pill draw at a racetrack. So, I have my set (30) I want to generate a random, once that random has been generated I don't want it to be able to be generated again until I restart the program.

Any ideas would be greatly appreciated, if I haven't been clear I am sorry.

Thanks

[404 byte] By [leachjaa] at [2007-11-27 11:29:45]
# 1

I guess I will try to clarify my original post

Once the random number is generated, lets say, I generate a 13 (out of 31 possible)

The next person to come through will hit the number to generate a random, if he gets a value that hasn't been handed out yet, all is well, if he happens to get a 13, it should generate another random, do the check again, and if it generates a previous value, iterates until a valid value is taken. If he gets a valid value on the second attempt, all is well.

leachjaa at 2007-7-29 16:29:52 > top of Java-index,Java Essentials,Java Programming...
# 2

huh?

TuringPesta at 2007-7-29 16:29:52 > top of Java-index,Java Essentials,Java Programming...
# 3

that's not random. Random means you can get repetition (though if the sample size is large enough in relation to the length of the series drawn the chances of getting repetition go down).

What you want is however provided in the API using the nextInt() method of the Random class.

Of course if you draw more numbers than are available in the sequence you will get repetition of numbers, that's inevitable.

jwentinga at 2007-7-29 16:29:52 > top of Java-index,Java Essentials,Java Programming...
# 4

So you want to choose random numbers from 1 to 31 without

the same number being chosen twice?

You can either store every chosen number in a Collection and

brute force it until the random number generator picks an

unpicked number.

Or each time you can decrease the range 1-31, 1-30, 1-29...

And then count from the beginning skipping the already chosen

values. Youll never have to pick more than one random number.

TuringPesta at 2007-7-29 16:29:52 > top of Java-index,Java Essentials,Java Programming...
# 5

Place all numbers into a set, shuffle them and then begin drawing numbers out.

floundera at 2007-7-29 16:29:52 > top of Java-index,Java Essentials,Java Programming...