Mail Archives: djgpp/1997/06/05/06:28:51
Francois Charton wrote:
> We say that random() is better than rand() becauses there are tests which
> rand() is known to fail that random() passes. But, once more, these
> correspond to very subtle "non randomness", in 99.99% of the cases,
> including all game programming, you can consider rand() and random() to
> be as good as each other.
Actually, for the most common use (rand() % n) they are not equally as
good. The problem with rand() is that the bottom bits are not at all
random, in fact they are a fairly short sequence especially if 'n' is
a power of 2 (i.e. extracting the bottom bits). I believe it just
alternates even and odd numbers, for instance.
With a better RNG like random() there is no difference between the
randomness of any bits compared to any other. For instance, nothing
is gained by doing (random()>>8)%16 compared to random()%16.
> As for the property you quote: each number having the same frequency,
> this is verified on average by almost all generators (including rand()
> and random()).
And, of course, by a repeating sequence within the range. For instance:
int randNoInRange(int range)
{
static int r = 0;
return (r++ % range);
}
fulfils that criterion. It fails several other tests, though...
Chris
- Raw text -