On Mon, 25 Aug 2008 14:47:03 -0700 (PDT),
<> wrote:
> On Aug 25, 3:35 pm, Rafael Cunha de Almeida <n...@email.provided>
> wrote:
>> Hi,
>>
>> I've found several sites on google telling me that I shouldn't use
>>
>> rand() % range+1
....
>> They fail to make it clear why. There seems to be less randomness in the
>> lower bits or something like that. I don't know what less randomness
>> would mean. Would it not be normally distributed or something like that?
>
> If rand() is implemented with a linear congruential generator f(x)=ax
> +b mod m, then it is a relatively easy automated check that the lower
> bits are in general highly correlated (given the first, the second is
> somewhat predictable).
....
> It's moderately unusual to use a high-quality RNG to implement rand().
"High-quality" is a bit too vague here. Are you saying that rand() is
still broken in the low-order bits in many libraries? (Yeah, "broken"
is also vague, but you know what I mean.)
The Linux rand(3) manual says this:
The versions of rand() and srand() in the Linux C Library use
the same random number generator as random() and srandom(), so
the lower-order bits should be as random as the higher-order
bits. However, on older rand() implementations, and on current
implementations on different systems, the lower-order bits are
much less random than the higher- order bits. Do not use this
function in applications intended to be portable when good
randomness is needed.
but I have never seen anyone explicitly list over current
implementation with this problem. I kind of assumed they were gone by
now. Or at least, I wouldn't be surprised if they were, and what used
to be a fact ended up as urban legent.
See also a discussion in and around Message-ID:
<> back in 2003.
/Jorgen
--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.se> R'lyeh wgah'nagl fhtagn!