-
July 28th, 2010, 04:46 AM
#1
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
#2
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
#3
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
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
July 29th, 2010, 04:45 PM
#4
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.
-
July 30th, 2010, 08:29 AM
#5
Re: Random Number Generator help!
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
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
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
July 30th, 2010, 10:06 AM
#6
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|