Mail Archives: djgpp/1999/01/22/22:24:08
On Thu, 21 Jan 1999, FireFoX McLouD wrote:
> How can I redefine the RAND_MAX parameter to use rand() with low
> numbers?
You can't. Even if you do redefine RAND_MAX, the behavior of `rand'
won't change, since `rand' was compiled with the original definition of
RAND_MAX.
The simplest way to get random integers from 0 to N is to say this:
rand()%(N+1)
But beware: for small values of N you will get not-so-random numbers,
because the low-order bits of what `rand' produces are known to be not so
random.
There are better ways, if the last caveat bothers you. One of them is to
use `random' instead of `rand'. For others, you will have to find a good
book on random numbers, it's too complicated to tell it here.
- Raw text -