Park–Miller random number generator

From Wikipedia, the free encyclopedia

The Park–Miller random number generator (or the Lehmer random number generator) is a variant of linear congruential generator that operates in multiplicative group of integers modulo n. A general formula of a random generator (RNG) of this type is:

X_{k+1} = X_k\cdot g~~\bmod~~n

where n is a prime number or a power of a prime number, g is an element of high multiplicative order modulo n (e.g., a primitive root modulo n), and X0 is co-prime to n.

Contents

[edit] Parameters in common use

Park and Miller suggested particular values n = 231 − 1 = 2147483647 (a Mersenne prime M31) and g = 16807 (a primitive root modulo M31). The Park–Miller RNG with these parameters is a build-in RNG in Apple CarbonLib. ZX Spectrum uses the Park–Miller RNG with parameters n = 216 + 1 = 65537 (a Fermat prime F4) and g=75 (a primitive root modulo F4). GNU Scientific Library uses the Park–Miller RNG with n = 248 and g = 44485709377909. Another popular pair of parameters is n = 4294967291 and g = 279470273.

[edit] Relation to LCG

While the Park–Miller RNG can be viewed as a particular case of the linear congruential generator with c = 0, it is a special case that implies certain restrictions and properties. In particular, for the Park–Miller RNG, the initial seed X0 must be co-prime to the modulus n that is not required for LCGs in general. The choice of the modulus n and the multiplier g is also more restrictive for the Park–Miller RNG. In difference from LCG, the maximum period of the Park–Miller RNG equals n-1 and it is such when n is prime and g is a primitive root modulo n.

On the other hand, the discrete logarithms (to base g or any primitive root modulo n) of Xk in \mathbb{Z}_n represent linear congruential sequence modulo Euler totient φ(n).

[edit] Sample C99 code

Using C99 code, this is written as follows:

#include <stdint.h>

uint32_t lcg_rand (uint32_t a)
{
    return ((uint64_t)a * 279470273) % 4294967291;
}

[edit] References