Preventing random number duplicates??
I am trying to generate 5 unique random numbers, but am finding that my method(s) every once in a while will create duplicate numbers. In truth, I expected this to happen, but my attempts at preventing this have failed. This is my partial code:
randNum_1 = 1 + (arc4random() % 59);
randNum_2 = 1 + (arc4random() % 59);
if (randNum_2 == randNum_1) {
randNum_2 = 1 + (arc4random() & 59);
}
It's obvious that there is nothing to prevent the second randNum_2 from being a duplicate of randNum_1 again and this is my problem. I have tried the following code with no success:
while ( randNum_2 == randNum_1) {
randNum_2 = 1 + (arc4random() & 59);
}
The while stmt generated a new randNum_2 whether it was necessary or not.
Any suggestions or comments will be greatly appreciated. Thanks.
|