GO-Ve01
ServicesDevelopersBooksDictionaryPapersWikidataEncyclopediaSearchBabelCareersDeck

Lehmer random number generator

The Lehmer random number generator (named after D. H. Lehmer), sometimes also referred to as the Park–Miller random number generator (after Stephen K. Park and Keith W. Miller), is a type of linear congruential generator (LCG) that operates in multiplicative group of integers modulo n. The general formula is : X_{k+1 = a \cdot X_k \bmod m, where the modulus m is a prime number or a power of a prime number, the multiplier a is an element of high multiplicative order modulo m (e.g., a primitive root modulo n), and the seed X is coprime to m. Other names are multiplicative linear congruential generator (MLCG) and multiplicative congruential generator (MCG). Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.

The Lehmer random number generator (named after D. H. Lehmer), sometimes also referred to as the Park–Miller random number generator (after Stephen K. Park and Keith W. Miller), is a type of linear congruential generator (LCG) that operates in multiplicative group of integers modulo n. The general formula is X_{k+1} = a \cdot X_k \bmod m, where the modulus m is a prime number or a power of a prime number, the multiplier a is an element of high multiplicative order modulo m (e.g., a primitive root modulo n), and the seed X is coprime to m. Other names are multiplicative linear congruential generator (MLCG) and multiplicative congruential generator (MCG). In 1988, Park and Miller suggested a Lehmer RNG with particular parameters m = 2 − 1 = 2,147,483,647 (a Mersenne prime M ) and a = 7 = 16,807 (a primitive root modulo M ), now known as MINSTD. Although MINSTD was later criticized by Marsaglia and Sullivan (1993), it is still in use today (in particular, in CarbonLib and C++11's minstd_rand0 ). Park, Miller and Stockmeyer responded to the criticism (1993), saying: Given the dynamic nature of the area, it is difficult for nonspecialists to make decisions about what generator to use. "Give me something I can understand, implement and port... it needn't be state-of-the-art, just make sure it's reasonably good and efficient." Our article and the associated minimal standard generator was an attempt to respond to this request. Five years later, we see no need to alter our response other than to suggest the use of the multiplier a = 48271 in place of 16807. This revised constant is used in C++11's minstd_rand random number generator. The Sinclair ZX81 and its successors use the Lehmer RNG with parameters m = 2  + 1 = 65,537 (a Fermat prime F ) and a = 75 (a primitive root modulo F ). The CRAY random number generator RANF is a Lehmer RNG with the power-of-two modulus m = 2 and a = 44,485,709,377,909. The GNU Scientific Library includes several random number generators of the Lehmer form, including MINSTD, RANF, and the infamous IBM random number generator RANDU. Using a composite modulus is possible, but the generator must be seeded with a value coprime to m, or the period will be greatly reduced. For example, a modulus of F = 2  + 1 might seem attractive, as the outputs can be easily mapped to a 32-bit word 0 ≤ X  − 1 < 2 . However, a seed of X  = 6700417 (which divides 2  + 1) or any multiple would lead to an output with a period of only 640. Another generator with a composite modulus is the one recommended by Nakazawa & Nakazawa: m = × = ≈ 2 a = (any of ±a (mod m) will do as well) As both factors of the modulus are less than 2 , it is possible to maintain the state modulo each of the factors, and construct the output value using the Chinese remainder theorem, using no more than 64-bit intermediate arithmetic. A more popular implementation for large periods is a combined linear congruential generator; combining (e.g. by summing their outputs) several generators is equivalent to the output of a single generator whose modulus is the product of the component generators' moduli. and whose period is the least common multiple of the component periods. Although the periods will share a common divisor of 2, the moduli can be chosen so that is the only common divisor and the resultant period is (m  − 1)(m  − 1)···(m  − 1)/2 . One example of this is the Wichmann–Hill generator. When the modulus is a Mersenne prime (d = 1), the procedure is particularly simple. Not only is multiplication by d trivial, but the conditional subtraction can be replaced by an unconditional shift and addition. To see this, note that the algorithm guarantees that , meaning that x = 0 and x = m are both impossible. This avoids the need to consider equivalent e-bit representations of the state; only values where the high bits are non-zero need reduction. The low e bits of the product ax cannot represent a value larger than m, and the high bits will never hold a value greater than a − 1 ≤ m − 2. Thus the first reduction step produces a value at most m + a − 1 ≤ 2m − 2 = 2 e+1  − 4. This is an (e + 1)-bit number, which can be greater than m (i.e. might have bit e set), but the high half is at most 1, and if it is, the low e bits will be strictly less than m. Thus whether the high bit is 1 or 0, a second reduction step (addition of the halves) will never overflow e bits, and the sum will be the desired value. If d > 1, conditional subtraction can also be avoided, but the procedure is more intricate. The fundamental challenge of a modulus like 2 32  − 5 lies in ensuring that we produce only one representation for values such as 1 ≡ 2 32  − 4. The solution is to temporarily add d, so that the range of possible values is d through 2 e  − 1, and reduce values larger than e bits in a way that never generates representations less than d. Finally subtracting the temporary offset produces the desired value. Begin by assuming that we have a partially reduced value y bounded so that 0 ≤ y < 2m = 2 e+1  − 2d. In this case, a single offset subtraction step will produce 0 ≤  < m. To see this, consider two cases: 0 ≤ y < m = 2 e − d In this case, y + d < 2 e and  = y < m, as desired. m ≤ y < 2m In this case, 2 e ≤ y + d < 2 e+1 is an (e + 1)-bit number, and  = 1. Thus,  = (y + d) − 2 e  + d − d = y − 2 e  + d = y − m < m, as desired. Because the multiplied high part is d, the sum is at least d, and subtracting the offset never causes underflow. (For the case of a Lehmer generator specifically, a zero state or its image y = m will never occur, so an offset of d − 1 will work just the same, if that is more convenient. This reduces the offset to 0 in the Mersenne prime case, when d = 1.) Reducing a larger product ax to less than 2m = 2 e+1  − 2d can be done by one or more reduction steps without an offset. If ad ≤ m, then one additional reduction step suffices. Since x < m, ax < am ≤ (a − 1)2 e , and one reduction step converts this to at most 2 e  − 1 + (a − 1)d = m + ad − 1. This is within the limit of 2m if ad − 1 < m, which is the initial assumption. If ad > m, then it is possible for the first reduction step to produce a sum greater than 2m = 2 e+1  − 2d, which is too large for the final reduction step. (It also requires the multiplication by d to produce a product larger than e bits, as mentioned above.) However, as long as d 2  < 2 e , the first reduction will produce a value in the range required for the preceding case of two reduction steps to apply. To use Schrage's method, first factor , i.e. precompute the auxiliary constants and = (m−r)/a. Then, each iteration, compute . This equality holds because \begin{align} aq &= a \cdot (m - r) / a \\ &= (a / a) \cdot (m - r) \\ &= (m - r) \\ &\equiv -r \pmod m \\ \end{align} so if we factor x = (x mod q) + q , we get: \begin{align} ax &= a(x \bmod q) + aq\lfloor x/q \rfloor \\ &= a(x \bmod q) + (m-r)\lfloor x/q \rfloor \\ &\equiv a(x \bmod q) - r\lfloor x/q \rfloor \pmod m \\ \end{align} The reason it does not overflow is that both terms are less than m. Since x mod q uint32_t lcg_parkmiller(uint32_t *state) { return *state = (uint64_t)*state * 48271 % 0x7fffffff; } This function can be called repeatedly to generate pseudorandom numbers, as long as the caller is careful to initialize the state to any number greater than zero and less than the modulus. In this implementation, 64-bit arithmetic is required; otherwise, the product of two 32-bit integers may overflow. To avoid the 64-bit division, do the reduction by hand: uint32_t lcg_parkmiller(uint32_t *state) { uint64_t product = (uint64_t)*state * 48271; uint32_t x = (product & 0x7fffffff) + (product >> 31); x = (x & 0x7fffffff) + (x >> 31); return *state = x; } To use only 32-bit arithmetic, use Schrage's method: uint32_t lcg_parkmiller(uint32_t *state) { // Precomputed parameters for Schrage's method const uint32_t M = 0x7fffffff; const uint32_t A = 48271; const uint32_t Q = M / A; // 44488 const uint32_t R = M % A; // 3399 uint32_t div = *state / Q; // max: M / Q = A = 48,271 uint32_t rem = *state % Q; // max: Q - 1 = 44,487 int32_t s = rem * A; // max: 44,487 * 48,271 = 2,147,431,977 = 0x7fff3629 int32_t t = div * R; // max: 48,271 * 3,399 = 164,073,129 int32_t result = s - t; if (result or use two 16×16-bit multiplies: uint32_t lcg_parkmiller(uint32_t *state) { const uint32_t A = 48271; uint32_t low = (*state & 0x7fff) * A; // max: 32,767 * 48,271 = 1,581,695,857 = 0x5e46c371 uint32_t high = (*state >> 15) * A; // max: 65,535 * 48,271 = 3,163,439,985 = 0xbc8e4371 uint32_t x = low + ((high & 0xffff) > 16); // max: 0x5e46c371 + 0x7fff8000 + 0xbc8e = 0xde46ffff x = (x & 0x7fffffff) + (x >> 31); return *state = x; } Another popular Lehmer generator uses the prime modulus 2 −5: uint32_t lcg_rand(uint32_t *state) { return *state = (uint64_t)*state * 279470273u % 0xfffffffb; } This can also be written without a 64-bit division: uint32_t lcg_rand(uint32_t *state) { uint64_t product = (uint64_t)*state * 279470273u; uint32_t x; // Not required because 5 * 279470273 = 0x5349e3c5 fits in 32 bits. // product = (product & 0xffffffff) + 5 * (product >> 32); // A multiplier larger than 0x33333333 = 858,993,459 would need it. // The multiply result fits into 32 bits, but the sum might be 33 bits. product = (product & 0xffffffff) + 5 * (uint32_t)(product >> 32); product += 4; // This sum is guaranteed to be 32 bits. x = (uint32_t)product + 5 * (uint32_t)(product >> 32); return *state = x - 4; } Many other Lehmer generators have good properties. The following modulo-2 128 Lehmer generator requires 128-bit support from the compiler and uses a multiplier computed by L'Ecuyer. It has a period of 2 126 : static unsigned __int128 state; /* The state must be seeded with an odd value. */ void seed(unsigned __int128 seed) { state = seed > 64; } The generator computes an odd 128-bit value and returns its upper 64 bits. This generator passes BigCrush from TestU01, but fails the TMFn test from PractRand. That test has been designed to catch exactly the defect of this type of generator: since the modulus is a power of 2, the period of the lowest bit in the output is only 2 62 , rather than 2 126 . Linear congruential generators with a power-of-2 modulus have a similar behavior. The following core routine improves upon the speed of the above code for integer workloads (if the constant declaration is allowed to be optimized out of a calculation loop by the compiler): uint64_t next(void) { uint64_t result = state >> 64; // GCC cannot write 128-bit literals, so we use an expression const unsigned __int128 mult = (unsigned __int128)0x12e15e35b500f16e However, because the multiplication is deferred, it is not suitable for hashing, since the first call simply returns the upper 64 bits of the seed state.

References

  1. Lehmer random number generator. https://en.wikipedia.org/w/index.php?oldid=1374725161 · 1795438144.7002156
source: e01 Encyclopedia · licence: CC BY-SA 4.0 · page · source revision

Cite this

"Lehmer random number generator". 1789321302. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1374725161

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·