Random Number Generator help!
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Member
Join Date
Aug 2009
Posts
52

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]);
}```

2. Elite Member Power Poster
Join Date
May 2006
Location
UK
Posts
4,473

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.

3. Elite Member Power Poster
Join Date
Aug 1999
Location
UK
Posts
10,163

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

4. Elite Member Power Poster
Join Date
May 2006
Location
UK
Posts
4,473

Re: Random Number Generator help!

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.

5. Elite Member Power Poster
Join Date
Aug 1999
Location
UK
Posts
10,163

Re: Random Number Generator help!

Originally Posted by keang
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.
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

6. Elite Member Power Poster
Join Date
May 2006
Location
UK
Posts
4,473

Re: Random Number Generator help!

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.

7. Elite Member Power Poster
Join Date
Aug 1999
Location
UK
Posts
10,163

Re: Random Number Generator help!

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

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

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•