Robert Hartley
Robert Hartley

Reputation: 33

Java select from a pool of random numbers

I'm making a bingo like program in java, I was wondering if it was a ll possible to select a number from a pool, then have it cross it out. I was thinking of putting the 75 (bingo numbers) into an array then have it select it from there, but i cant seem to find a way to get rid of that number once it's selected. for example, i only want to call the number 55 ONCE, then have it gone, or non-accessible from the pool once it's called by my random function.

Thanks Rob

Upvotes: 1

Views: 1983

Answers (4)

jdw6415
jdw6415

Reputation: 11

Place all 75 numbers into an array.

Call Arrays.shuffle() on the array.

Read the array in-order.

Upvotes: 1

zch
zch

Reputation: 15278

  1. Generate array 1..75.
  2. Shuffle.
  3. Read one at the time.

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html#shuffle%28java.util.List%29

Upvotes: 4

splungebob
splungebob

Reputation: 5415

  1. Create a Collection of Integers
  2. Randomly generate an int with a range from zero to collection.size()-1
  3. Remove the item at the index of the random int from step 2. This item is the number you call and will no longer be selectable.

Upvotes: 0

Colleen
Colleen

Reputation: 25489

(deleted my previous answer because I misread the question)

Easiest way I can think of to do this is to store them in an ArrayList, track the size and feed that into a random number generator to randomly access an index and remove after use.

Upvotes: 1

Related Questions