Random number generator

Gary Jackoway gary at hpavla.HP.COM
Sat Dec 23 01:58:29 AEST 1989


> I was trying to generate a random number bewteen 0 and maxlongint (i.e. 2^32)
> on my machine (80386 with Interactive). The rand() function that I have
> returns a psuedo-random number between 0 and MAXINT (which in this case
> is 32768). 
----------
Simply put two rand's together:

       longrand = (unsigned long) rand() | ((unsigned long) rand() << 32);

This should give you a uniform distribution.
I should warn you that rand() is often NOT a good random number generator,
so if the application is sensitive, be sure to run randomness tests on
your long random numbers.

No charge,

Gary Jackoway



More information about the Comp.lang.c mailing list