same random number should not generate again and a

2019-09-07 08:37发布

问题:

Possible Duplicates:
C++ random number generator without repeating numbers
Unique random numbers in O(1)?

I am doing as following

int y = arc4random() % 50;

I am using objective-c Now as I don't want same number again and again, like if 6 is once I get, then I don't need 6 again, because I am calling this line again and again and taking random numbers.

回答1:

How about you generate an array (1,2,3,4,5,6,...) and sort it randomly, then read the array elements one at a time.

Otherwise, if you use a random number generator and make sure it only gives you numbers you haven't seen before, you can only call it a limited number of times.

sorting randomly will depend on what language you're using.