# Random Number Generator help!

• July 28th, 2010, 04:46 AM
hugo84
Random Number Generator help!
Hi i am trying to print 6 numbers randomly..however i do not want any of the numbers to be a duplicate. Could you help me see what is wrong with my codes/flow? as i am still getting duplicates. Thanks in advance.

Code:

```boolean donPrintFlag = false; //create random object to use             Random r2 = new Random();             //create array object             int arrayNum[] = new int[7];             for (int i = 0; i < 6; i++) {                 int randNum3 = r2.nextInt(45) + 1;                 //initialise 6 numbers into array                 arrayNum[i] = randNum3;                 //  System.out.println("array print " + arrayNum[i]);                 //loop through all numbers to find duplicate                 for (int j = 1; j < 6; j++) {                     if (arrayNum[i] == arrayNum[j]) {                         donPrintFlag = true;                     }                     else{                         donPrintFlag = false;                     }                 }                 //print non duplicate numbers                 if (!donPrintFlag) {                     System.out.println("Number" + i + ": " + arrayNum[i]);                 }```
• July 28th, 2010, 07:25 AM
keang
Re: Random Number Generator help!
There are several ways of doing this, such as:

• If there aren't many numbers: fill an ArrayList with the numbers (ie all the numbers from 1 to 10) and call the Collections.shuffle() method to randomise the list. Finally pull out the first 'n' numbers.
• Create a Set and fill it with random numbers until it's size is the required size (ie 6), then use an iterator to extract the numbers.
• Or, if you really have to use an array, check the number is not already in the array before you add it.
• July 29th, 2010, 09:50 AM
dlorde
Re: Random Number Generator help!
Alternatively, generate random numbers and add them to a Set (e.g. HashSet). A Set guarantees no duplicates.

The ability to simplify means to eliminate the unnecessary so that the necessary may speak...
H. Hofmann
• July 29th, 2010, 04:45 PM
keang
Re: Random Number Generator help!
Quote:

Alternatively, generate random numbers and add them to a Set (e.g. HashSet). A Set guarantees no duplicates.
Err isn't using a Set what I suggested in my second point?

Although with a HashSet you may lose some of the randomness, depending on how the numbers get distributed into the buckets and the order in which they are returned by the iterator. Using a LinkedHashset maintains the generation order so probably should be used instead.
• July 30th, 2010, 08:29 AM
dlorde
Re: Random Number Generator help!
Quote:

Originally Posted by keang
Err isn't using a Set what I suggested in my second point?

Yes, sorry - I was in a rush, speed-reading the thread :o

Quote:

Although with a HashSet you may lose some of the randomness, depending on how the numbers get distributed into the buckets and the order in which they are returned by the iterator. Using a LinkedHashset maintains the generation order so probably should be used instead.
That's an interesting point - I wonder if it does make a difference... but as you say, LinkedHashSet would be the safer bet.

The one who insists on never uttering an error must remain silent...
W. Heisenberg
• July 30th, 2010, 10:06 AM
keang
Re: Random Number Generator help!
Quote:

That's an interesting point - I wonder if it does make a difference.
The reason I suggested it maybe an issue is as follows:

Assume you generate 2 series of random numbers which happen to contain the same numbers but in a different order eg 7, 98, 68, 27 and 27, 98, 7, 68. If each number happens to be in a different HashSet bucket then both series will be returned as identical sequences by the iterator eg 27, 98, 68, 7.

I've tested this to prove it on my system and these numbers do get reordered to the same sequence.
• August 1st, 2010, 01:54 PM
dlorde
Re: Random Number Generator help!
Quote:

Originally Posted by keang
I've it on my system and these numbers do get reordered to the same sequence.

OK, I'convinced :thumb:

Good thinking. I must be getting old ;)

Being abstract is something profoundly different from being vague... The purpose of abstraction is not to be vague, but to create a new semantic level in which one can be absolutely precise...
E. Dijkstra