Multiply-with-carry pseudorandom number generator
In computer science, multiply-with-carry is a method invented by George Marsaglia for generating sequences of random integers based on an initial set from two to many thousands of randomly chosen seed values. The main advantages of the MWC method are that it invokes simple computer integer arithmetic and leads to very fast generation of sequences of random numbers with immense periods, ranging from around 260 to 22000000.
As with all pseudorandom number generators, the resulting sequences are functions of the supplied seed values.
General theory
An MWC generator is a special form of Lehmer random number generator xn = bxn−1 mod p which allows efficient implementation of a prime modulus p much larger than the machine word size.Normal Lehmer generator implementations choose a modulus close to the machine word size. An MWC generator instead maintains its state in base b, so multiplying by b is done implicitly by shifting one word. The MWC multiplier a and lag r determine the modulus p = abr−1. a is chosen so the modulus is prime and the multiplier has long period.
The base b is typically chosen to equal the computer's word size, as this makes arithmetic modulo b trivial. This may vary from b = 28 for a microcontroller to b = 264.
Sometimes an odd base is preferred, in which case b = 2k − 1 can be used, which is almost as simple to implement.
The period of a lag-r MWC generator is the order of b in the multiplicative group of numbers modulo p = abr − 1. While it is theoretically possible to choose a non-prime modulus, a prime modulus eliminates the possibility of the initial seed sharing a common divisor with the modulus, which would reduce the generator's period.
Because 2 is a quadratic residue of numbers of the form 8k±1, b = 2k cannot be a primitive root of p = abr − 1. Therefore, MWC generators with base 2k have their parameters chosen so their period is /2. This is one of the difficulties that use of b = 2k − 1 overcomes.
The basic form of an MWC generator has parameters a, b and r, and r+1 words of state. The state consists of r residues modulo b
and a carry cr−1 < a.
The initial state values are arbitrary, except that they must not be all zero, nor all at the maximum permitted values.
The lag-r MWC sequence is then a sequence of pairs
xn, cn determined by
and the MWC generator output is the sequence of xs,
Although the theory of MWC generators permits a > b, a is almost always chosen smaller for convenience of implementation.
The state transformation function of an MWC generator is one step of Montgomery reduction modulo p. The state is a large integer with most significant word cn−1 and least significant word xn−r. Each step, xn−r· is added to this integer. This is done in two parts: −1·xn−r is added to xn−r, resulting in a least significant word of zero. And second, a·xn−r is added to the carry. This makes the integer one word longer, producing two new most significant words xn and cn.
So far, this has simply added a multiple of p to the state, resulting in a different representative of the same residue class modulo p. But finally, the state is shifted down one word, dividing by b. This discards the least significant word of zero and effectively multiplies the state by b−1.
Thus, a multiply-with-carry generator is a Lehmer generator with modulus p and multiplier b−1. This is the same as a generator with multiplier b, but producing output in reverse order, which does not affect the quality of the resultant pseudorandom numbers.
This in turn means that the mathematical techniques developed for such generators can be applied to multiply-with-carry generators.
Efficiency
A linear congruential generator with base b = 232 is implemented aswhere c is a constant.
If a ≡ 1 and c is odd, the resulting base-232 congruential sequence will have period 232.
This can be computed using only the low 32 bits of the product of a and the current x.
However, many microprocessors can compute a full 64-bit product in almost the same time as the low 32 bits. Indeed, many compute the 64-bit product and then ignore the high half.
A lag-1 multiply-with-carry generator allows us to make the period nearly 263 by using almost the same computer operations, except that the top half of the 64-bit product is not ignored after the product is computed.
Instead, a 64-bit sum is computed, and the top half is used as a new carry value c rather than the fixed additive constant of the standard congruential sequence:
Compute ax+c in 64 bits, then use the top half as the new c, and bottom half as the new x.
Choice of multiplier
With multiplier a specified,each pair of input values x, c is converted to a new pair,
If x and c are not both zero, then the period of the resulting multiply-with-carry sequence will be the order of b = 232
in the multiplicative group of residues modulo
abr − 1, that is, the smallest n such that
bn ≡ 1.
If p = abr − 1 is prime, then Fermat's little theorem guarantees that the order of any element must divide p − 1 = abr − 2, so one way to ensure a large order is to choose a such that p is a "safe prime",
that is both p and /2 = abr/2 − 1 are prime. In such a case, for b= 232 and r = 1, the period will be abr/2 − 1, approaching 263, which in practice may be an acceptably large subset of the number of possible 32-bit pairs.
More specifically, in such a case, the order of any element divides p − 1, and there are only four possible divisors: 1, 2, abr/2 − 1, or abr − 2. The first two apply only to the elements 1 and −1, and quadratic reciprocity arguments show that the fourth option cannot apply to b, so only the third option remains.
Following are some maximal values of a for computer applications which satisfy the above safe prime condition, for lag-1 generators:
Bits in a | b | Maximum a such that ab−1 is a safe prime | Period |
15 | 216 | 215−50 = 32,718 | 1,072,103,423 |
16 | 216 | 216−352 = 65,184 | 2,135,949,311 |
31 | 232 | 231−563 = 2,147,483,085 | 4,611,684,809,394,094,079 |
32 | 232 | 232−178 = 4,294,967,118 | 9,223,371,654,602,686,463 |
64 | 264 | 264−742 = 18,446,744,073,709,550,874 | 263−1 ≈ |
128 | 2128 | 2128−10,408 | 2127−1 ≈ |
256 | 2256 | 2256−9166 | 2255−1 ≈ |
512 | 2512 | 2512−150,736 | 2511−1 ≈ |
While a safe prime ensures that almost any element of the group has a large order, the period of the generator is specifically the order of b. For small moduli, more computationally expensive methods can be used to find multipliers a where the period is ab/2 − 1.
The following are again maximum values of a of various sizes.
Bits in a | br | Maximum a such that b has order abr/2−1 | Period |
8 | 28 | 28−7 = 249 | 31,871 |
8 | 2 = 216 | 28−32 = 224 | 7,340,031 |
15 | 216 | 215−29 = 32,739 | 1,072,791,551 |
16 | 216 | 216−22 = 65,514 | 2,146,762,751 |
8 | 4 = 232 | 28−64 = 192 | 412,316,860,415 |
15 | 2 = 232 | 215−26 = 32,742 | 70,312,909,602,815 |
16 | 2 = 232 | 216−2 = 65,534 | 140,733,193,388,031 |
31 | 232 | 231−68 = 2,147,483,580 | 4,611,685,872,398,499,839 |
32 | 232 | 232−76 = 4,294,967,220 | 9,223,371,873,646,018,559 |
8 | 8 = 264 | 28−41 = 215 | 263−1 ≈ |
15 | 4 = 264 | 215−50 = 32,718 | 263−1 ≈ |
16 | 4 = 264 | 216−56 = 65,480 | 263−1 ≈ |
31 | 2 = 264 | 231−38 = 2,147,483,610 | 263−1 ≈ |
32 | 2 = 264 | 232−43 = 4,294,967,253 | 263−1 ≈ |
63 | 264 | 263−140 = 9,223,372,036,854,775,668 | 263−1 ≈ |
64 | 264 | 264−116 = 18,446,744,073,709,551,500 | 263−1 ≈ |
MWC generators as repeating decimals
The output of a multiply-with-carry generator is equivalent to the radix-b expansion of a fraction with denominator p = abr − 1. Here is an example for the simple case of b = 10 and r = 1, so the result is a repeating decimal.Starting with, the MWC sequence
produces this sequence of states:
with period 22. Consider just the sequence of xi:
Notice that if those repeated segments of x values are put in reverse order:
we get the expansion
j/ with a=7, b=10, j=10:
This is true in general: The sequence of xs produced by a lag-r MWC generator:
when put in reverse order, will be the radix-b expansion of a fraction j/ for some 0 < j < abr''.
Equivalence to linear congruential generator
Continuing the above example, if we start with, and generate the ordinary congruential sequencewe get the period 22 sequence
and that sequence, reduced mod 10, is
the same sequence of xs resulting from the MWC sequence.
This is true in general, :
Given initial values, the sequence resulting from the lag-1 MWC sequence
is exactly the Lehmer random number generator output sequence yn = ayn − 1 mod,
reduced modulo b.
Choosing a different initial value y0 merely rotates the cycle of xs.
Complementary-multiply-with-carry generators
Establishing the period of a lag-r MWC generator usually entails choosing multiplier a so that p = abr − 1 is prime. Then p − 1 will have to be factored in order to find the order of b mod p. If p is a safe prime, then this is simple, and the order of b will be either p − 1 or /2. In other cases, p − 1 may be difficult to factor.However, the algorithm also permits a negative multiplier. This leads to a slight modification of the MWC procedure, and produces a modulus p = = abr + 1. This makes p − 1 = abr easy to factor, making it practical to establish the period of very large generators.
The modified procedure is called complementary-multiply-with-carry, and the setup is the same as that for lag-r MWC: multiplier a, base b, and r + 1 seeds,
The modification is to the generation of a new pair. Rearranging the computation to avoid negative numbers, the new x value is complemented by subtracting it from b − 1:
The resulting sequence of xs produced by the CMWC RNG will have period the order of b in the multiplicative group of residues modulo abr+1, and the output xs, in reverse order, will form the base b expansion of j/ for some 0 < j < abr.
Use of lag-r CMWC makes it much easier to find periods for rs as large as 512, 1024, 2048, etc.
Another advantage of this modified procedure is that the period is a multiple of b, so the output is exactly equidistributed mod b.
One disadvantage of the CMWC construction is that, with a power-of-two base, the maximum achievable period is less than for a similar-sized MWC generator; you lose several bits. Thus, an MWC generator is usually preferable for small lags. This can remedied by using b = 2k−1, or choosing a lag one word longer to compensate.
Some examples:
With b = 232, and a = 109111 or 108798 or 108517, the period of the lag-1024 CMWC
will be a·232762 = ab1024/64, about 109867.
With b = 232 and a = 3636507990, p = ab1359 − 1 is a safe prime, so the MWC sequence based on that a has period 3636507990·243487 ≈ 1013101.
With b = 232, a CMWC RNG with near record period may be based on the prime p = 15455296b42658 + 1. The order of b for that prime is 241489·21365056 ≈ 10410928.
More general moduli
The MWC modulus of abr−1 is chosen to make computation particularly simple, but brings with it some disadvantages, notably that the period is at most half the modulus. There are several ways to generalize this, at the cost of more multiplications per iteration.First, it is possible to add additional terms to the product, producing a modulus of the form arbr+asbs−1. This requires computing cnb + xn = arxn−r + asxn−s.
However, this does not fix the period issue, which depends on the low bits of the modulus. Fortunately, the Montgomery reduction algorithm permits other moduli, as long as they are relatively prime to the base b, and this can be applied to permit a modulus of the form arbr−a0, for a wide range of values a0. To do this, precompute a , and change the iteration as follows:
In the common case that b = 2k, a0 must be odd for the inverse to exist.
Implementation
The following is an implementation of the CMWC algorithm in the C programming language. Also, included in the program is a sample initialization function. In this implementation the base is 232−1 and lag r=4096. The period of the resulting generator is about.// C99 Complementary Multiply With Carry generator
- include
- include
- include
- include
- define CMWC_CYCLE 4096 // as Marsaglia recommends
- define CMWC_C_MAX 809430660 // as Marsaglia recommends
// Make 32 bit random number
uint32_t rand32
// Init the state with seed
void initCMWC
// CMWC engine
uint32_t randCMWC //EDITED parameter *state was missing
int main