问题
I am using rand()
for a 6 digit field which needs unique values. Am I doing it right?
What are the odds, rand()
can give me similar values on consecutive or frequent calls?
It was unique when I used rand(). But, returned same number when I called srand(time(NULL))
or srand(clock())
. Seems, like it's working opposite for me. Or is it?
回答1:
As others have pointed out, uniqueness is not guaranteed. However you are probably seeing repeated numbers because you are using srand() and rand() incorrectly.
srand() is used to seed the random number generator. that means a series of calls to rand() after a call to srand will produce a particular series of values. If you call srand() with the same value then rand() will produce the same series of values (for a given implementation, there's no guarantee between different implementations)
int main() {
srand(100);
for(int i = 0; i<5; ++i)
printf("%d\n",rand());
printf("\nreset\n\n");
srand(100);
for(int i = 0; i<5; ++i)
printf("%d\n",rand());
}
for me this produces:
365
1216
5415
16704
24504
reset
365
1216
5415
16704
24504
time() and clock() return the time, but if you call them quickly enough then the value returned will be the same, so you will get the same series of values out of rand().
Additionally rand() is generally not a very good random number generator and using it usually means you have to transform the series of numbers to the distribution you actually need. You should find a different source of randomness and either learn the proper ways to produce the distribution you want or use a library that can do it for you. (for example one common method of producing a 'random' number between 0 and N is to do rand() % N
but this is not really the best method.
C++ provides a much better random number library in <random>
. It provides different PRNG algorithms, such as linear_congruential, mersennne_twister, and possibly even a cryptographically secure RNG (depending on the implementation). It also provides objects for producing a variety of distributions, such as uniform_int_distribution which should avoid the mistake make in rand() % N
.
回答2:
rand() returns values between 0 and RAND_MAX
. Since it's a discrete uniform distribution, you have a probability of 1/(RAND_MAX
+1) of repeating a number and therefore, uniqueness is not guaranteed.
srand(seed)
initializes your random number generator so that the sequence of numbers obtained from rand()
is the same every time given that you initialize it with seed
every time.
In your example seed = time(NULL) which is the number of seconds elapsed from January 1, 1970 thus ensuring a different seed, and thus a different sequence of random numbers for every call to srand(time(NULL))
(assuming that it's not made within the same second).
回答3:
Random numbers are random, not unique. Just like a situation with throwing dice when you can roll several sixes in a row, your rand
can (and should) sometimes give you runs of identical numbers.
In order to make sure that the numbers are unique, build a set where you register each of the numbers that you have added already. When a random number comes up more than once, throw the second one away, and go for the next one.
回答4:
What are the odds, rand() can give me similar values on consecutive or frequent calls?
The algorithm of rand
is unspecified in C. So is the quality of the randomness of the numbers returned by rand
.
来源:https://stackoverflow.com/questions/12412108/how-unique-is-rand-in-c