From the linux rand(3) man page:
Quote:
The versions of rand() and srand() in the Linux C Library use the same random number gen*
erator as random() and srandom(), so the lower-order bits should be as random as the
higher-order bits. However, on older rand() implementations, the lower-order bits are
much less random than the higher-order bits.
In Numerical Recipes in C: The Art of Scientific Computing (William H. Press, Brian P.
Flannery, Saul A. Teukolsky, William T. Vetterling; New York: Cambridge University Press,
1992 (2nd ed., p. 277)), the following comments are made:
"If you want to generate a random integer between 1 and 10, you should always do
it by using high-order bits, as in
j=1+(int)(10.0*rand()/(RAND_MAX+1.0));
and never by anything resembling
j=1+(rand() % 10);
(which uses lower-order bits)."
|
In other words, under Linux your algorithm is probably fine, on other architectures it may not be very random. For portability, use the noted syntax or check the man page for your particular rand implementation.