random numbers


erez_zi
12-24-2002, 01:26 AM
Hi,

I'm not sure if this is the right place to ask this question:
I saw the following code somewhere:
myrandom(void)
{
static int dirty=2;

dirty *= 69069;
dirty += 85;
return dirty;
}

I didn't understand the usage of 69069. I guess it has something to do with the fact that the I use 32 bit numbers. How?

Thanks
Erez

Bill Press
12-26-2002, 09:44 AM
Hi, Erez.

Yes, this only works if the multiplication and the addition both operate modulo 2^32, which will be true in many, but not all, C implementations.

The multiplier 69069 is an ancient one that was used, e.g., in DEC VMS Fortran. The constant 85 doesn't matter much. One often sees 0 or 1.

However, the most important point is that this is NOT a particularly good random number generator. See Chapter 7 in the NR books (2nd Edition) for much better and equally portable choices.

Cheers,
Bill P.

erez_zi
12-29-2002, 12:59 AM
thanks

tchristney
07-29-2003, 03:32 PM
see also Mersenne Twister (http://www.math.keio.ac.jp/~matumoto/emt.html)