Manuscript Offprint
arXiv:2610.27313Scrambled Multiply-with-Carry Pseudorandom Number Generators
SEBASTIANO VIGNA, Università degli Studi di Milano, Italy
Multiply-with-carry (MWC) generators are very fast pseudorandom number generators whose quality depends on the choice of a multiplier, and is usually assessed by applying the spectral test to an associated multiplicative congruential generator. We show that the special form of the modulus imposes structural limitations on the lattice structure of every MWC generator, which the generalized MWC generators of Goresky and Klapper mitigate; in our tests, xoring each output with its carry removes them. Since these limitations make the usual worst-case criterion useless, we rank candidates by a weighted geometric mean of their figures of merit, which does not depend on their normalization, and suggest multipliers for generators with 64 to 256 bits of state. Along the way, we correct some errors in the literature. In the end, we propose MWC192X, a 64-bit generator with 192 bits of state that passes all tests we are aware of, and emits an output in half a nanosecond.
CCS Concepts: • Mathematics of computing → Random number generation.
Additional Key Words and Phrases: Pseudorandom number generators, multiply-with-carry, spectral test
1 Introduction
Congruential generators are among the oldest and best understood pseudorandom number generators (PRNGs): their outputs have a lattice structure that can be analyzed precisely with number-theoretic tools [17]. This paper is about a family of very fast generators whose analysis reduces to that of congruential generators: generators with carry.
Marsaglia and Zaman [31] introduced in 1991 add-with-carry (AWC) and subtract-with-borrow (SWB) generators, which extend lagged Fibonacci generators by propagating a carry (or borrow). Tezuka, L'Ecuyer, and Couture [37] showed that they are, in disguise, multiplicative congruential generators (MCGs) with a huge modulus, whose lattice structure is very bad in dimensions larger than the lag (see also [3]), a defect that Lüscher's RANLUX generator [28] mitigates by discarding most of the outputs.
In 1994, Marsaglia [29] proposed multiply-with-carry (MWC) generators, in which the sum of two lagged values is replaced by a linear combination of lagged values. Couture and L'Ecuyer [4, 5] showed that they, too, simulate an MCG: when only one coefficient is nonzero, if is the base (typically, or ) and is the lag, the modulus is and the multiplier is . They also introduced complementary MWC generators, with modulus , later popularized by Marsaglia [30]. Independently, Klapper and Goresky [14, 15] studied feedback with carry shift registers (FCSRs), the binary analogue of MWC generators, using 2-adic numbers. Goresky and Klapper [12] later merged the two points of view, and proposed a generalized MWC (GMWC) generator, in which the new digit is multiplied by a coefficient coprime with the base [16], so that the modulus can have a much less constrained form.
On 64-bit architectures with fast -bit multiplications, MWC generators are a very interesting choice: an MWC generator with 128 bits of state needs one full multiplication and one addition, and has period about ; when the lag is larger than one, the multiplications of consecutive outputs are independent, and modern processors execute them in parallel (Section 8.3). The author has been distributing MWC and GMWC generators for a few years, and since version
Author's Contact Information: Sebastiano Vigna, Università degli Studi di Milano, Dipartimento di Informatica, Milano, Italy, vigna@acm.org.
Sebastiano Vigna
25.0 Erlang/OTP includes a 59-bit MWC generator whose multiplier was selected by the author and Raimo Niskanen [9] (Section 8.1).
The quality of an MWC generator depends on the multiplier , and the natural tool to assess it is the spectral test [6, 17] of the associated MCG, which computes, in each dimension , the maximal distance between successive hyperplanes over all families of equidistant parallel hyperplanes covering the points produced by the generator. The distances are normalized into figures of merit , which are summarized by a single criterion, such as their minimum, the worst-case figure of merit [10, 19], or the harmonic score of Steele and Vigna [36]. The form of the modulus of an MWC generator, however, forces a bad lattice structure: in dimensions up to the figures of merit are fixed by the parameters, and in dimension they are, as we show, smaller than for , so the worst-case figure of merit is determined by a single, structurally bad dimension.
Our main result makes this limitation explicit: whatever the lag and the multiplier , the vectors of outputs of an MWC generator lie, up to an error smaller than in each coordinate, in a family of equidistant parallel planes at least about apart (Section 4.2; the argument is similar to that used by L'Ecuyer and coauthors for multiple recursive generators with few nonzero coefficients [26, 27]). The resulting bounds on the figures of merit are nearly attained by our best multipliers and, together with a generalization, explain all the structural limitations we observe. A three-dimensional birthday-spacings test detects the defect in a few seconds for , but for it needs about 3 TB of output and an implementation that can process them independently of the available memory: in our opinion, this makes the defect irrelevant in practice.
GMWC generators with lag larger than one have a similar defect, with planes about apart, whereas for GMWC generators with lag one we find multipliers with excellent figures of merit in all the dimensions we consider. In any case, the scrambler X, which generalizes to GMWC generators that of Thomas's MWC64X generator (the exclusive or of each digit with the corresponding carry), removes, in our tests, the defect and every other form of lattice structure we could detect (Section 5).
Since the worst-case figure of merit is useless, we rank candidates by a weighted geometric mean of their figures of merit: by a classical characterization from social choice theory [7], this is essentially the only ranking that depends neither on the normalization of the figures of merit nor on the other candidates (Section 6). We report the results of a search over more than 52 million candidates for nine combinations of base, lag, and generator type, and suggest multipliers. Our final suggested generator is MWC192X: on x86-64 processors, MWC192X and MWC256X are the fastest scalar generators known to the author that have a known period and jump functions, and pass all the statistical tests we are aware of (Section 8.4). We suggest also 32-bit generators.
We give a self-contained presentation of (G)MWC generators based on b-adic numbers, following Goresky and Klapper [12], with simple seeding instructions for GMWC generators, and we correct three errors in [12]: the formula for the carry is incorrect when , three of the twelve proposed parameter sets do not have maximum period, and the exponential representation of the output has the wrong sign (a related error appears in [13]). Finally, our title nods to the scrambled linear generators of Blackman and Vigna [1]: our generators, too, are made of an engine and a scrambler, but the engine is based on congruential arithmetic rather than on -linear maps, and, dually, our scrambler uses -based rather than congruential arithmetic.
The examined candidate multipliers and their spectral data are available at https://vigna.di.unimi.it/MWC.tar.zst.
where the initial state is given by digits , , ..., and by a carry . Since is invertible modulo , reading (1) modulo shows that is uniquely determined as , and then is uniquely determined, too. The state of the generator at time is , and the generator outputs the sequence of digits , , , ... . The recurrence (1) is the special case of the general recurrence studied by Goresky and Klapper [12] in which only the coefficients of and are nonzero. The fundamental parameter associated with the generator is the modulus, which Goresky and Klapper call connection integer:
Note that , so is coprime with . We will always assume .
When , the recurrence (1) becomes
which is an MWC generator [5, 29] with a single nonzero coefficient (as in [30]) and modulus . When and , the computation of requires a -bit multiplication and a 2w-bit addition; and are then just the lower and upper bits of the result.
Complementary MWC generators [4, 5, 30] correspond instead to the case : for example, Marsaglia's complementary generator computes and , and a direct computation shows that , that is, the recurrence (1) with and carry . Their modulus is .
In this paper, a GMWC generator will be a generator with , where (odd, as will be a power of two); complementary MWC generators are thus the case , and all our results about GMWC generators apply to them. In particular, since , complementary MWC generators have the same structural limitations as MWC generators (Section 4.2), and when their modulus cannot be a safe prime, as is even. We will be interested, instead, in large values of . We use the name GMWC for the family of Goresky and Klapper, who simply call these generators MWC generators. In this case
and . The computation now requires an additional -bit multiplication (by A), an additional -bit multiplication (by e), and an additional 2w-bit addition. Note that both multiplications lie on the dependency chain of the carry (see Section 8.3). We restrict ourselves to negative because in that case all carries of strictly periodic states are nonnegative (see Section 3.7), which makes it possible to implement the generator using unsigned arithmetic.
The formula for the carry given by Goresky and Klapper [12] is not correct. In their notation, the state is , where is the most recent digit, and the next digit and carry satisfy , where (in our case, this is (1)). They then state [12, Equations (3) and (4)] that and . The first expression for is the definition solved for , but the second one is false in general: since ,
Implementations may return the oldest or the newest digit of the state, but the output sequence is in both cases the sequence of digits, possibly shifted by k - 1 positions.
Sebastiano Vigna
#define MWC_A2 0xff4e24dd418f5433 /* Seed: 0 < mwc_c < MWC_A2 - 1 (we suggest mwc_c = 1) */ static uint64_t mwc_x, mwc_y, mwc_c;
static inline uint64_t mwc192x_next(void) { const uint64_t result = mwc_y ^ mwc_c; /* Scrambler X */ const __uint128_t t = MWC_A2 * (__uint128_t)mwc_x + mwc_c; mwc_x = mwc_y; mwc_y = t; mwc_c = t >> 64; return result; }
#define GMWC_A1 0xffff0cfb4ed6366fa #define GMWC_MINUSA0 0x68935f57 /* e / #define GMWC_A0INV 0x8316d3ad5de20399 / (-e)^-1 mod 2^64 / / Seed: GMWC_MINUSA0 <= gmwc_c <= GMWC_A1 (a sufficient condition, not an invariant; we suggest gmwc_c = 2 * GMWC_MINUSA0) */ static uint64_t gmwc_x, gmwc_c;
static inline uint64_t gmwc128x_next(void) { const __uint128_t t = GMWC_A1 * (__uint128_t)gmwc_x + gmwc_c; gmwc_x = GMWC_A0INV * (uint64_t)t; gmwc_c = (t + GMWC_MINUSA0 * (__uint128_t)gmwc_x) >> 64; return gmwc_x ^ (uint64_t)(t >> 64); /* Scrambler X */ }
Fig. 1. The C code for MWC192 and GMWC128 with the scrambler X of Section 5, which are the generators we suggest (Section 8.4); returning mwc_y and the new value of gmwc_x gives the plain generators. The type __uint128_t is a GCC/Clang extension; other compilers provide equivalent intrinsics (e.g., _umul128). Seeding is discussed in Section 3.7.
the two expressions differ by , which is zero if and only if . This is always true when , as in Marsaglia's MWC generators, but if , as in our GMWC generators, the correct carry is strictly larger than whenever . The same formula appears in the later monograph [13, Chapter 4], whereas the definition of algebraic feedback shift registers of Klapper and Xu [16] computes the carry correctly. The proofs in [12] use the correct expression (e.g., Equation (9) in the proof of their Theorem 3.1): the error affects only implementations following Equation (4) literally.
We will name generators by their state size: for , MWC128 has , MWC192 has , and MWC256 has , and similarly for GMWC generators. Generators with will be denoted by a /32 suffix (e.g., MWC64/32 has and ). Figure 1 shows a C implementation of MWC192 and GMWC128 with the scrambler X of Section 5.
3 The arithmetic of (G)MWC generators
We now recall, with complete proofs, the basic properties of (G)MWC generators, following the b-adic approach of Goresky and Klapper [12], which gives very short proofs.
The error has been confirmed by Mark Goresky (personal communication, October 2020).
3.1 b-adic numbers
We recall a few basic facts about -adic numbers; we refer the reader to the monograph by Goresky and Klapper [13], where they are called -adic numbers, for proofs and details. A -adic integer is a formal series with ; we call its digits. The -adic integers form a ring , in which sums and products are computed propagating carries, exactly like in base- arithmetic, except that numbers are infinite to the left.
The ring contains (e.g., ), and, more generally, all rationals whose denominator is coprime with ; the map from such rationals to is injective. A series of -adic integers converges if the number of trailing zero digits of goes to infinity; in particular, converges for every sequence of integers . When , is the ring of 2-adic integers, and the base- digits of are its bits grouped in blocks of .
The digits of a rational number in are eventually periodic, and the fundamental fact we will use is the following characterization of strictly periodic expansions, which is the b-adic analogue of [15, Theorem 2.1]:
LEMMA 3.1. Let be coprime with and . Then, the -adic expansion of is strictly periodic if and only if .
PROOF. Let and . If , then ; since , the expansion of is the -digit base- representation of repeated forever. Conversely, if the expansion is strictly periodic with some period , and is the integer represented by the first digits, then , that is, .
The proof shows also that the -adic digits of are the digits of the ordinary base- fractional expansion of in reverse order, which is why the classical analyses [5, 37], based on fractional expansions, describe carry generators as producing the digits of a rational number “in reverse order” (see also [30]).
3.2 The state integer
The fundamental observation is that the output of a (G)MWC generator is the b-adic expansion of a rational number with denominator m, whose numerator can be computed from the state:
LEMMA 3.2 ([12], LEMMA 6.1). Let be a state of the generator, let be the integer whose base-b digits are , and let
Then, in .
PROOF. Let . Multiplying (1) by and summing over we obtain
whence , that is, .
We call the state integer. Note that for an MWC generator the state integer is simply the number with base- digits , that is, the state read as a -digit number with the carry as most significant digit. For a GMWC generator, . As noted by Goresky and Klapper, the state integer determines the state: reading (2) modulo we recover successively , and then .
Sebastiano Vigna
3.3 The associated MCG
We say that a state is strictly periodic if the generator returns to it, and that it is degenerate if the output is constant. Since the state integer determines the state, and the map from rationals to is injective, the generator returns to a state if and only if the output sequence from that state is strictly periodic; thus, by Lemmas 3.1 and 3.2, a state is strictly periodic if and only if . The all-zero state ( ) is degenerate. The state with all digits equal to and carry (the value of the carry is computed from (1) with ) has , because , and is degenerate, too.
THEOREM 3.3. Let , consider a generator started from a state with , and for let be the state integer of the state . Then, for all :
(1) and ;
(2) ;
(3) .
Proof. It is sufficient to prove the statements for . Since the next state outputs , by Lemma 3.2 we have , that is,
an equation among integers. Reading it modulo gives ; reading it modulo , and using , gives , that is, . The initial state is strictly periodic, and so is the next state, so ; if , then (3) implies , and if then (3) implies , so and . Finally, , so .
Thus, the state integers are generated by an MCG with modulus and multiplier , the analogue of the LCG representation of AWC and SWB generators [37]; Theorem 3.3 corresponds to Theorem 1 of [5] in the MWC case, and to Theorems 2.2 and 4.1 of [12] in general (the sign of in [12, Theorem 2.2] is incorrect, see Section 3.6). For MWC generators , and when we have , so : the state integers of a lag-one MWC generator are generated by the MCG with modulus and multiplier [30].
3.4 Connections with algebraic recurrences
The output of a linear feedback shift register (LFSR) over with connection polynomial is the power-series expansion of a rational function , whereas the output of a (G)MWC generator is the b-adic expansion of a rational number . When , since , a (G)MWC generator with modulus computes, bits at a time, the bit sequence of a binary FCSR with connection integer [14, 15], in the same way as word-oriented linear generators compute many steps of an LFSR at once. Algebraic feedback shift registers [16] provide a common generalization (see [13] for a comprehensive treatment), and the second statement of Theorem 3.3 is the analogue of the trace representation of -sequences, called exponential representation [15, 16].
3.5 Period
By Theorem 3.3, if is prime the period of the output sequence of a generator started from a strictly periodic nondegenerate state is . The period is , the maximum possible, if and only if is a primitive root modulo . As observed by Couture and L'Ecuyer [5, Section 3] (see also [12]), when and with this never happens: indeed, , so is a quadratic residue modulo (and so is ). Moreover, if is even is a square, so for or the period of any (G)MWC generator is at most .
This bound is easily attained: if is a safe prime, that is, both and are prime, then is a divisor of ; since is a quadratic residue, divides the prime , and since we conclude that . In this case, the nonzero residues modulo are split into two orbits of size (the quadratic residues and the quadratic nonresidues), and every nondegenerate strictly periodic state has period . Safe primes were already used for this purpose by Marsaglia, as reported by Couture and L'Ecuyer [5, p. 605]. All generators considered in this paper have a safe-prime modulus, whose primality (and that of ) we have proved for all multipliers we suggest.
If is a safe prime, then is odd, so and is a quadratic nonresidue: the map exchanges the two orbits, and since and , the outputs of the two orbits are digit-wise complements of each other. In every dimension, thus, the points of the two orbits lie in the same families of parallel hyperplanes, and, as is customary when the generator has several cycles [37, Section 5.3], we will study the lattice structure of the points produced by all cycles together.
3.6 Errata
Besides the formula for the carry discussed in Section 2, [12] contains two other errors. The first one is a sign error in the exponential representation of the output. Theorem 2.2 of [12] states that the output of a strictly periodic state with state integer h (defined as in Lemma 3.2) is
where and , but by Theorem 3.3 the correct sign is positive. The problem is in the proof: reading the identity modulo , Goresky and Klapper use , whereas their definition of the connection integer, , gives . The error is visible already for MWC generators: for , , , and we have , and the state has state integer and output 1, 2, 4, 8, 6, ..., whereas the formula gives 9 as first output. The same sign error occurs in the proof of Theorem 4.1 of [12] (when reading their Equation (10) modulo ), so the map used in that theorem, and defined in [12, Section 2], should be .
In the later monograph [13, Chapter 4] the output is written as the expansion of , where , but the representation is stated as , with the reduction modulo in : since , this formula yields the digit-wise complement of the actual output. Since the digit-wise complement of the output has the same period and the same distribution of tuples of outputs, the other results of [12] are not affected.
The second error concerns the table of parameters. Goresky and Klapper [12, Section 7] correctly observe that is a primitive root modulo a prime if and only if 2 is a primitive root modulo and is coprime with , and then propose a table of twelve parameter sets, with , claimed to yield period . However, three of the parameter sets have an even , which cannot be coprime with . We verified using Sage that for all twelve parameter sets is a (probable) prime and 2 is a primitive root modulo . Thus, the nine parameter sets with odd are correct, but for the two parameter sets with the period is , and for the parameter set with (where ) the period is (see Table 1). Mark Goresky confirmed that the three parameter sets should not have been included. The claims about the distribution of tuples of outputs in [12, Theorem 2.4] do not apply to these three parameter sets.
Over a single orbit of an MWC generator, the excess of zeroes over ones in each output bit is the class number of [5, Section 3], a negligible quantity for our moduli.
Personal communication, October 2020.
Sebastiano Vigna
Table 1. The parameter sets of [12, Section 7] that do not have maximum period. The values of and are as in [12].
| b | p | m | Claimed period | Actual period |
|---|---|---|---|---|
| m - 1 | ||||
| m - 1 | ||||
| m - 1 |
3.7 Seeding
A user must be able to seed a generator easily, avoiding degenerate states and states that are not strictly periodic. For an MWC generator this is simple: since the state integer is the state read as a number, if and only if , and, if is a prime larger than (as in all our generators), the only degenerate states in this range are the all-zero state and the state with all digits equal to and carry . Thus, it is sufficient to choose the digits arbitrarily and the carry so that , excluding the all-zero state (this condition is slightly conservative, as it excludes all states with carry ). These facts are classical: the range of the carry of strictly periodic states is given by Couture and L'Ecuyer [5, Corollary 3].
For GMWC generators the situation is more complex, as the state integer is not the state read as a number. The degenerate states are again the all-zero state and the state with all digits equal to and carry , but states lying "between" these two states (as numbers) are not necessarily strictly periodic: for example, if and then . Theorem 2.1 of [12] characterizes strictly periodic states in terms of the state integer (as we did above), but checking whether the state integer of a state is in is cumbersome. The following sufficient condition is much more practical:
PROPOSITION 3.4. Consider a GMWC generator with . Then, every state whose carry satisfies has state integer . In particular, if is prime, all such states are strictly periodic, nondegenerate, and have period .
PROOF. Since , we have , and . The second statement follows from Theorem 3.3.
The condition of Proposition 3.4 excludes some strictly periodic states (e.g., if , the state with , , and all other digits equal to zero, for which ), but in practice the loss is negligible: there are states satisfying the condition, out of the nondegenerate strictly periodic states. If (as it will happen for all our multipliers) and (see below), then , so we lose less than of the states.
As for the range of the carry, if then . Thus, if , the carry can be stored in a single -bit word, and the intermediate results and fit into bits, as assumed by the code in Figure 1.
Finally, since the state integers evolve as an MCG, states whose state integers are in a simple arithmetic relation produce outputs in the same relation. In particular, the carry of an MWC generator should never be set to zero: in that case the state integer is , so, for example, the outputs of MWC128 seeded with and are, modulo and up to a difference of at most two, twice
A degenerate state with constant output has , that is, ; if is coprime with this implies , so or . Without this hypothesis there may be other degenerate states.
The condition of Proposition 3.4 is a condition for seeding, not an invariant: strictly periodic states may have carries outside , and a routine validating a state (e.g., one restored from a file) should rather check that .
This result is also contained in Theorem 3.1 of [12] and in [13, Chapter 4]; for an MWC generator, the range is .
and three times the outputs of the generator seeded with . We thus suggest to set the carry to one: the state integer then lies in , so no state integer is a small multiple of another.
The same problem occurs for a GMWC generator with carry , whose state integer is then congruent to modulo , and with carry , whose state integer is ; the carry , which places the state integer in , avoids it.
In all cases, however, generators seeded with consecutive values of X and the same carry have state integers in arithmetic progression, so the second difference of the outputs of three such generators is, modulo b, always in : the state should be derived from the seed using a good mixing function (e.g., SplitMix64 [35]), mapping the carry into the allowed range, or parallel streams should be obtained by jumping.
3.8 Jumping ahead
Theorem 3.3 provides an immediate way to jump ahead, as already observed for AWC and SWB generators by Tezuka, L'Ecuyer, and Couture [37, Section 3]: to advance a generator by J steps, one computes the state integer h, multiplies it by the precomputed constant , and recovers the state from the result. For MWC generators the first and last step are trivial, as the state integer is just the state read as a number. For GMWC generators we have , and given h we recover the digits by (for , ) and the carry by . The only cost is a modular multiplication on -bit integers; jumping backwards is equally easy, as . With a jump of J steps one obtains non-overlapping streams (e.g., about streams of length for MWC128).
The code distributed by the author, which uses the multipliers suggested in Section 8, can jump by , by (where ), and by an arbitrary number of steps (by modular exponentiation).
4 The lattice structure of (G)MWC generators
4.1 The spectral test
Consider an MCG with modulus m and multiplier , and let
be the set of all vectors of successive values produced by the generator from all initial states (including the zero vector). Then, , where is the lattice generated by and by the vector ; since contains with index , its determinant is . The maximal distance between successive hyperplanes, over all families of equidistant parallel hyperplanes covering , is , where is the Euclidean length of a shortest nonzero vector in the dual lattice
indeed, every nonzero defines the family of hyperplanes , , which are apart and cover . Computing is the spectral test [6, 17].
The dual lattice has determinant . The Hermite constant is the smallest constant such that every -dimensional lattice with determinant contains a nonzero vector of length at most ; thus,
Sebastiano Vigna
or, equivalently, . Following L'Ecuyer [19], we use the normalized figure of merit
(L'Ecuyer writes and for and ): values close to one denote a good lattice structure, whereas values close to zero denote a bad one. For the Hermite constants are known exactly [2]: , , , , , , and . Since the normalization hides the actual distance between hyperplanes, we will also report : the hyperplanes are at distance .
More generally, given a set of indices with , one can apply the spectral test to the projection
of on the coordinates in , which, if is not of the form , contains vectors of non-successive values, or vectors with lacunary indices [18, 23]. The dual lattice of the projection is formed by the vectors such that , and we denote by the length of its shortest nonzero vectors, and by the corresponding distance between hyperplanes; thus, . Note that the vectors of whose coordinates outside are zero are exactly the vectors of , extended with zeroes, so whenever ; in particular, is nonincreasing in [17].
By Theorem 3.3, a (G)MWC generator simulates the MCG with multiplier : the output differs from by less than , so vectors of successive outputs are, up to an error smaller than in each coordinate, points of for the multiplier [5, Theorem 1], and the same is true of any projection. Multiplying the congruence defining by we see that is just with coordinates in reverse order (the reverse sequence has the same lattice structure [37, Section 4]), so we can (and will) compute figures of merit using the more convenient multiplier .
The dual lattice contains two kinds of short vectors. Letting be the -th vector of the canonical basis, , the vectors
belong to , because ; and, if , the vector
belongs to , because (and so do its shifted copies ). Couture and L'Ecuyer [3, 5] used these vectors to show that in dimensions up to the figures of merit are essentially fixed by and . The first statement of the following proposition is Theorem 3(i) of [5] (see also Theorem 1 of [3]):
PROPOSITION 4.1. Assume (which is true whenever , or and ). For , , and thus . Moreover, , and thus .
Proof. For we have , that is, . The proof of Proposition 1 of [36], which uses only the definition of the dual lattice, shows that in this case the shortest nonzero vectors of have length . The second statement follows from .
Table 2. Minimum and maximum value of the figures of merit over all candidates for . Grayed-out dimensions are those in which the figure of merit is essentially fixed (the maximum value is less than 1.003 times the minimum value).
| Generator | ||||||||
|---|---|---|---|---|---|---|---|---|
| MWC128 | min | 0.929 | 0.013 | 0.044 | 0.069 | 0.092 | ||
| max | 0.931 | 0.946 | 0.936 | 0.902 | 0.876 | 0.849 | ||
| MWC192 | min | 0.889 | 0.052 | |||||
| max | 0.891 | 0.012 | 0.948 | 0.267 | 0.850 | |||
| MWC256 | min | 0.838 | ||||||
| max | 0.841 | 0.038 | 0.864 | |||||
| GMWC128 | min | 0.929 | 0.011 | 0.028 | 0.036 | 0.063 | 0.072 | |
| max | 0.931 | 0.988 | 0.950 | 0.935 | 0.905 | 0.881 | 0.847 | |
| GMWC256 | min | 0.838 | 0.103 | 0.105 | ||||
| max | 0.841 | 0.046 | 0.870 | 0.836 | ||||
Since , for the figure of merit is approximately , a very small number, whereas the bound on is approximately , and it is attained under mild hypotheses for MWC generators [5, Theorem 3], and empirically for our GMWC generators (Table 2). The bad lattice structure in dimensions is not a real problem: the distance between hyperplanes is about , the resolution of the output, as -tuples of successive outputs are the most significant digits of a single state integer divided by .
4.2 Dimension and lacunary triples
The real limitation of MWC generators appears in dimension . Couture and L'Ecuyer [5, Lemma 2] observed that, in dimensions larger than , short vectors appear when a small multiple of the shortest vector in dimension is close to , and showed that the eight nonzero coefficients of Marsaglia's generators with base can be chosen so as to avoid the phenomenon. When only one coefficient is nonzero, however, all coordinates of but two are zero, and the phenomenon cannot be avoided.
We now make this observation explicit and quantitative. For AWC and SWB generators, whose modulus has base-b digits in , a single dual vector with coordinates in shows that the points lie in planes at least apart [3, 18, 37]. For an MWC generator, instead, u has length about b, and short dual vectors arise only as combinations of u and , that is, as short vectors of a two-dimensional sublattice spanned by long vectors. Bounding such vectors uniformly in the multiplier is the technique used by L'Ecuyer and Touzin [27] and by L'Ecuyer and Simard [26] to show that, for multiple recursive generators with few nonzero coefficients, some projections with lacunary indices have a bad lattice structure whatever the coefficients; the order of magnitude of our bound can be derived also from Lemma 2 of [5] and Dirichlet's approximation theorem. To the best of our knowledge, however, these bounds have not been stated for (G)MWC generators, and the exact characterization of in the second statement of Proposition 4.2, which concerns successive values and not only a projection, the three-dimensional bound of Proposition 4.4 for GMWC generators, and its generalization in Section 4.3 are new.
In what follows, we let
Goresky and Klapper [13, Chapter 4], for example, only remark that for large dimensions the distribution of tuples of MWC sequences is expected to suffer from the same shortcomings as that of AWC and SWB generators.
Sebastiano Vigna
PROPOSITION 4.2. For every (G)MWC generator, , and thus
Moreover, if and , the shortest nonzero vectors of are the shortest nonzero vectors of the sublattice spanned by and , and thus
PROOF. In dimension , consider the vectors and . We have , , and , so the two-dimensional sublattice of they span has determinant with
By the definition of the Hermite constant, the sublattice contains a nonzero vector of length at most , and so does .
For the second statement, let be a shortest nonzero vector of , and . Since and , by the first statement , so ; moreover, , as . Let and . Since and , the integer is a multiple of . Using and , we obtain
so , and thus . If for some , choosing the largest such we would have , and thus , a contradiction. Hence, , and gives . Letting and , we conclude that , whose squared length is .
For MWC and complementary MWC generators , and since we have , so . Thus, using , for close to we obtain approximately
For , this is approximately for , for , and for . Intuitively, is almost parallel to , as both are dominated by their -th coordinate, and combining them one can cancel the large coordinates.
By the second statement of Proposition 4.2, for MWC generators depends only on and , and not on the lag, and it can be computed by reducing a two-dimensional lattice; since is attained only by a hexagonal lattice, the best multipliers are those for which this lattice is almost hexagonal. For an MWC generator, the approach of Couture and L'Ecuyer [5], who chose coefficients so that the multiples are far from for , amounts instead (dropping the bound on ) to maximizing the length of a shortest nonzero vector of the lattice generated by and , which ignores the coordinate of . Since and (by Lagrange's bound, ), we have , and thus ; but the proxy is not accurate enough for selection: on 20 000 candidates of our MWC128 search, the multiplier maximizing ranks 4 209th by , and the best is 17% larger.
The vectors used in the proof of Proposition 4.2 have a very specific support, and this fact has important consequences:
COROLLARY 4.3. Let . Then:
(1) for the multiplier , ; thus, for every initial state with , the vectors , , lie, up to an error smaller than in each coordinate, in a family of equidistant parallel planes at least apart;
(2) for every , , and thus .
Proof. The vectors and have support , so the proof of Proposition 4.2 shows that for and the multiplier . Reversing the coordinates in dimension , the indices for the multiplier correspond to the indices for the multiplier (Section 4.1), and the first claim follows from Theorem 3.3. The second statement follows from Proposition 4.2, as is nonincreasing in .
Under the hypotheses of the second statement of Proposition 4.2, its proof, applied to the vectors of (for which ), shows also that , so the planes of the first statement are exactly apart.
Thus, for every MWC or complementary MWC generator with , independently of the multiplier and of the lag, the vectors lie in a family of equidistant parallel planes at least about apart, that is, about for and for . A larger lag moves the defect to a projection with lacunary indices, and makes it invisible to the figures of merit of dimensions 3 to , but it does not make it smaller: the decrease of with is entirely due to the normalization by . The phenomenon is analogous to the bad lattice structure of vectors of non-successive values produced by recurrences with few nonzero coefficients [18, 26, 27], and to the defect of AWC and SWB generators in dimensions larger than their largest lag [37].
From a practical viewpoint, the distinction between successive and lacunary indices is not the relevant one: an application using outputs at each step sees the triples as naturally as triples of successive outputs. What matters is the amount of output a test needs to detect the defect, which depends on the distance between the planes, and thus on , but not on : as we show below, for the defect is detected in a few seconds, whereas for the test we describe needs about 3 TB of output. In our opinion, a defect of the latter kind is of no practical relevance (in any case, the scrambler X of Section 5 removes it), but this position is not universally shared: the figures of merit proposed by L'Ecuyer and Lemieux [23], for example, include projections with lacunary indices, independently of the amount of output needed to detect their lattice structure.
For the defect is easily detectable: Thomas already observed that the multipliers of MWC generators with and give a poor distribution of successive triples, and that such generators fail tests on triples. A textbook three-dimensional birthday-spacings test [17, 24] using the upper 20 bits of three successive outputs, with 10 repetitions of points (about outputs, a few seconds of computation), finds 1 029 collisions for MWC64/32 (with the multiplier suggested in Section 8), whereas 20 are expected. On MWC128/32 the same test finds the expected number of collisions on successive triples, but 1 088 on the lacunary triples , whereas GMWC64/32 and GMWC128/32 pass the test on both kinds of triples.
With a 64-bit modulus, however, lattice structure is detectable quickly also in generators without structural defects. The worst three-dimensional projection of GMWC64/32 with indices up to 24, , has (see Section 4.3), and the same test using the upper 22 bits of each
http://cas.ee.ic.ac.uk/people/dt10/research/rngs-gpu-mwc64x.html (2011).
Sebastiano Vigna
output and 10 repetitions of points (less than half a minute of computation) finds 91 collisions on the triples , whereas 20 are expected.
For the defect of MWC128 is much harder to detect. For the MWC128 multiplier of Table 6, we have , so successive planes are about 13 times farther apart than the side of a cell of a three-dimensional test using the upper 36 bits of each output (recall that outputs differ from the points of the associated MCG by less than ). Indeed, a three-dimensional birthday-spacings test on the upper 36 bits of triples of successive outputs (about 3 TB) finds 81 collisions, whereas 1.62 are expected ( ) [38]. A test of this size must process terabytes of output independently of the available memory, which the implementation of [38] does by trading memory for time, and must use cell numbers of more than 64 bits: for example, the birthday-spacings test of TestU01 [25] accepts at most cells, that is, about 21 bits per coordinate in three dimensions, and thus cannot see planes at distance about .
By Corollary 4.3, the lacunary triples of MWC192 and MWC256 lie in planes at essentially the same distance (see Section 4.3), so the same test applied to such triples might fail at a similar cost. The -tuples of successive outputs of MWC192 and MWC256 lie in families of hyperplanes at a similar distance, too, but a birthday-spacings test with the same resolution would need about and times more points, respectively.
Note that the congruence defining the planes holds modulo , up to a small error, so the defect is visible also in the lower bits: the test on the lowest 20 bits of MWC64/32 finds 1 058 collisions, whereas 20 are expected, and the test above on the lowest 36 bits of MWC128 finds 89, whereas 1.62 are expected.
For GMWC generators, instead, the first coordinate of is , and if is large the bound of Proposition 4.2 becomes vacuous: for this happens as soon as , approximately. There is, however, another sublattice that limits when :
PROPOSITION 4.4. For every (G)MWC generator with , , and thus
PROOF. In dimension , consider the vectors , , and . Since , and have disjoint supports, and . Moreover, and . The Gram determinant of the three vectors is thus
and we conclude as in the previous proof, using .
For GMWC generators with the bound on is approximately , up to a factor , which is much larger than the bound for MWC generators, but still very small: for it is approximately 0.023 for and for . For , is also the point at which the bound of Proposition 4.2 stops being stronger than the bound of Proposition 4.4; for MWC generators (at least when , as for all our multipliers) the bound of Proposition 4.2 is stronger. As in Corollary 4.3, the vectors used in the proof have support (a set invariant under reversal), so the bound applies to for , and to all with : the corresponding distance between hyperplanes is about , which is much smaller than the distance of MWC generators.
The expected number of collisions is proportional to divided by the number of cells, where is the number of points; using 36 bits per coordinate in dimension multiplies the number of cells by , and thus by .
Table 3. Upper bounds on from Propositions 4.2 (MWC) and 4.4 (GMWC with ), maximized over the range of multipliers we consider, versus the maximum value found in our search, and relative gap (bound divided by maximum, minus one).
| Generator | Best found | Bound | Gap | |
|---|---|---|---|---|
| MWC128 | 0.020% | |||
| MWC192 | 0.002% | |||
| MWC256 | 0.020% | |||
| GMWC256 | 1.03% | |||
| MWC64/32 | 0.0282 | 0.0282 | 0.22% | |
| MWC128/32 | 0.44% | |||
| GMWC128/32 | 0.0468 | 0.0474 | 1.24% |
Table 3 compares the bounds with the largest values of found by our search (see Section 7): the best multipliers are within of the bound for MWC generators with , within for MWC generators with , and within for GMWC generators, showing that the bounds are essentially tight.
4.3 Beyond dimension
Table 2 shows the minimum and maximum value of each figure of merit over all candidates of our search for . Structural limitations are visible also beyond dimension , and most of them are explained by Corollary 4.3: for instance, the bound gives for MWC192, whereas the observed maximum is 0.0118. The remaining limitations are explained by the following generalization of Propositions 4.2 and 4.4 (see also [3, Proposition 2]):
PROPOSITION 4.5. Assume that is coprime with (and thus with ; this is always true for MWC generators). Let be a set of at least two pairs of nonnegative integers such that the positions are distinct and smaller than , let , and let . Then,
PROOF. Since , we have . Thus, if is orthogonal to , the vector of with in position , and zeroes elsewhere, belongs to , as and is invertible modulo . The integer vectors orthogonal to form a lattice of rank and determinant , and we conclude as in the proof of Proposition 4.2.
When is coprime with , the first statement of Proposition 4.2 and Proposition 4.4 are the cases and . For example, for the set , with positions , gives for GMWC256, against an observed maximum of 0.046; for , the set , with positions , gives for MWC generators , and thus for MWC192, against an observed maximum of 0.267 (for the multiplier we suggest, the shortest nonzero vector of has exactly the support of this family). For the positions of these families collide, which is consistent with the absence of a structural limitation on for GMWC generators with lag one. Note that, in contrast, GMWC128 reaches a figure of merit of 0.988 in dimension three, whereas MWC128 cannot go beyond .
Table 4 reports, for the multipliers suggested in Section 8, the distance between hyperplanes in dimension ; the worst three-dimensional projection with indices up to 24, that is, the set
Sebastiano Vigna
Table 4. Distances between hyperplanes for the multipliers suggested in Section 8: ; ; the indices and of the worst three-dimensional projection with indices up to 24 and of the worst four-dimensional projection with indices up to 12; and . Indices refer to outputs.
| Generator | Worst 3-D | Worst 4-D | |||||
|---|---|---|---|---|---|---|---|
| MWC128 | 128 | 32.28 | 32.28 | 29.60 | 0.76 | ||
| MWC192 | 192 | 32.31 | 32.31 | 32.31 | 0.74 | ||
| MWC256 | 256 | 32.29 | 32.29 | 32.29 | 0.10 | ||
| GMWC128 | 128 | 42.73 | 38.99 | 28.67 | 0.83 | ||
| GMWC256 | 256 | 42.70 | 55.20 | 42.70 | 0.81 | ||
| MWC64/32 | 64 | 16.32 | 16.32 | 13.86 | 0.77 | ||
| MWC128/32 | 128 | 16.29 | 16.29 | 16.29 | 0.19 | ||
| GMWC64/32 | 64 | 21.43 | 19.08 | 14.09 | 0.88 | ||
| GMWC128/32 | 128 | 21.40 | 23.68 | 21.40 | 0.97 | ||
with minimizing , and the worst four-dimensional projection with indices up to 12, both computed exactly with fp111; and the minimum of for , which measures the quality of the lattice structure in higher dimensions without using Hermite constants (for the densest lattices, ). For all MWC generators the worst three-dimensional projection is exactly the one with indices of Corollary 4.3; for GMWC generators with , the worst four-dimensional projection is the one with indices of Proposition 4.4, with a distance between hyperplanes of about .
Distances in different dimensions are not directly comparable: the worst four-dimensional projections of MWC128 and GMWC128 have hyperplanes at distance and , larger than for MWC128, but this reflects the minimum over 220 projections rather than a structural defect ( is between 0.10 and 0.27 for all generators with , whereas for MWC128). The low values of for MWC256 and MWC128/32 () are attained in dimension , the smallest dimension containing the positions of the second family above.
5 Scrambling
The lattice structure studied in Section 4 is a property of the digits , which are, up to an error smaller than , the points of the associated MCG. Thomas's MWC64X generator (footnote 10), an MWC generator with and designed for GPUs, returns instead , the bitwise exclusive or of the digit and of the carry computed together with it by (1), that is, of the lower and upper halves of ; note that and are the two most significant base- digits of the state integer of the state , and for they are the whole state.
For GMWC generators, would be biased: by (1), , so the range of the carry is shifted by about , the upper bits of and are positively correlated, and the most significant bit of is one with probability about . The bias is negligible for GMWC128 (), but it is about for GMWC256, and for GMWC64/32 PractRand detects it after 4 GB of output. We thus define a scrambler that generalizes Thomas's, returning : since for MWC generators, it degenerates to Thomas's scrambler, so we simply call it X, and we denote scrambled generators by the suffix X (e.g., MWC192X and GMWC128X). For GMWC generators, is the state integer when , and differs by at most from the integer formed by its two most significant base- digits otherwise, and depends only on . Since and the carry fits in a w-bit word (Section 3.7), X costs a single instruction, which is not on the loop-carried dependency chain (Section 8.3; see Figure 1).
Thomas motivates his scrambler by noting that the carry changes completely at each step, and that the exclusive or is an operation of a different kind than those of the recurrence. More specifically, , so when is close to the upper bits of (for an MWC generator, ) are, up to a borrow, those of (for our multipliers, , so at least the upper eight bits): X combines with by an operation that does not preserve the linear relations with small integer coefficients that define the families of hyperplanes of the spectral test. For the same reason, X is useful only if the carry has about as many bits as the digit: the carry of mwc59 (Section 8.1) has 27 bits, so X would leave the upper five bits of its 32-bit digit unchanged.
Empirically, X removes from all bits every defect we could detect. For , with X the test of Section 4.2 finds the expected number of collisions, on the upper and on the lower bits, on the triples on which MWC64/32 and MWC128/32 fail, and X hides also the lattice structure due to the 64-bit modulus of GMWC64/32. More generally, on the upper and on the lower bits of the outputs with indices , , of these three generators and of GMWC128/32 with X, all 224 counts are within three standard deviations of the expected value. For , the test of Section 4.2 on triples of successive outputs of MWC128X finds 2 collisions both on the upper and on the lowest 36 bits, whereas 1.62 are expected ( ), and 81 and 89 are found without X.
As for every scrambler, it is a good idea to choose the best possible underlying generator, and we still select multipliers using the spectral test, in the same way in which the scrambled linear generators of Blackman and Vigna [1] use underlying generators with good -linear properties.
6 Selecting multipliers
6.1 Classical criteria
When choosing a multiplier for an MCG, the figures of merit , , ..., are usually summarized by a single criterion. The most common choice is the worst-case figure of merit , which was used by Fishman and Moore [10] and later by L'Ecuyer and coauthors [19, 21], who also considered worst cases over projections with lacunary indices [23].
Steele and Vigna [36, Section 9] observed that, when selecting among many candidates, the minimum is a questionable choice even for ordinary congruential generators, and gave the following example of two multipliers for a 64-bit LCG, with their figures of merit , , ..., :
0xe2e19bb27190da6d 0.791216 0.771300 0.791569 0.777944 0.773526 0.777463 0.766073
0xe73d20db8e96d2cd 0.941271 0.883251 0.854317 0.825078 0.803654 0.781546 0.766043
The minimum chooses the first multiplier, whose is larger in the fifth decimal place, although the second multiplier is much better in all other dimensions. Steele and Vigna thus consider as equivalent all multipliers whose minimum figure of merit is in the top 0.1% of the candidates, and among them choose the one with the best harmonic score
which gives more weight to low dimensions.
For (G)MWC generators both criteria break down. Figures of merit in dimensions up to are essentially fixed (Proposition 4.1); for MWC generators, and for GMWC generators with , the figure of merit in dimension (and in a few more dimensions) is structurally bounded by a very small quantity (Propositions 4.2 and 4.4). Thus, even ignoring the fixed dimensions, maximizing
The more common xorshift scrambler XS, , which the code of MWC128 previously distributed by the author suggested, does not fix the lower bits, as it leaves the lowest w/2 bits unchanged.
Sebastiano Vigna
the worst-case figure of merit means optimizing a single, structurally bad dimension, with no control on the others. The harmonic score has the opposite problem: the figures of merit in the bad dimensions are tiny, so they contribute nothing to the score.
The problem is that the absolute values of the figures of merit in different dimensions are not comparable: for MWC128, a value of equal to is excellent, whereas a value of equal to 0.6 is mediocre.
6.2 The weighted geometric mean
Ratios of figures of merit, however, are comparable: a candidate whose is 10% larger than that of another candidate is better in dimension three to the same extent as a candidate whose is 10% larger is better in dimension six. We thus look for a way of ranking candidates that does not depend on the normalization of each figure of merit (by Hermite constants, by the structural bounds of Section 4, or by the values observed on a set of candidates), and in which the comparison between two candidates depends only on their figures of merit. These requirements essentially determine the ranking, by a classical characterization from social choice theory, where the same invariance is required for utilities measured on ratio scales (Fleming and Wallace [11] use the same argument to summarize normalized benchmark results by their geometric mean):
PROPOSITION 6.1 ([7, THEOREMS 4.2 AND 4.17]). Let be a total preorder on such that:
(1) for every , the sets and are closed;
(2) if for every , then ;
(3) if , then for every .
Then, there are , not all zero, such that if and only if .
PROOF. Taking logarithms, rescalings become translations, and the statement becomes the characterization of weighted utilitarianism of d'Aspremont and Gevers [7, Theorem 4.2].
Proposition 6.1 leaves us free to choose the weights. We use the weights of the harmonic score , which give more importance to low dimensions, and rank candidates by the weighted geometric mean
that is, by the harmonic score of the logarithms of the figures of merit. We use , as is common in the literature [19, 36]. Since the ranking does not depend on the normalization, Hermite constants play no role, and in dimensions in which they are not known one can use in place of .
The harmonic score itself, instead, does not satisfy the third condition of Proposition 6.1: its choices depend on the normalization of the figures of merit. For example, dividing each figure of merit by its maximum over the candidates, rather than by , changes the choice of in six of our nine searches. Moreover, figures of merit that are very small contribute almost nothing to , independently of how much they vary in relative terms (see Section 6.3).
The weighted geometric mean has a natural interpretation: is the number of bits of resolution lost in dimension with respect to an optimal lattice with the same density, so maximizing minimizes a weighted total of the number of bits lost. In this view, minimizes the largest loss, which for MWC generators is determined by their structure (MWC128 loses more than 10 bits in dimension three, whatever the multiplier), whereas ignores the losses in dimensions in which is very small.
The candidate maximizing is Pareto optimal, as is strictly increasing in each figure of merit. Figures of merit essentially fixed by the structure of the generator multiply by essentially the same factor for all candidates, so they need not be excluded (on our data, excluding them does not change the choice). Unlike , compensates a loss in a dimension with gains in others, but only in relative terms: on our data, every figure of merit of the chosen candidates is at least 72% of the best value found in its dimension (82% except for GMWC128, see Section 6.3). The choice depends little on the weights: equal weights would change it only for GMWC128, and weights (i.e., equal weights on the logarithms of Knuth’s figures of merit [17]) only for GMWC128 and GMWC64/32.
The weighted geometric means of the five best candidates of each search differ by less than 1.3%, so they should be considered essentially equivalent (in Section 8 we report all of them).
6.3 Comparison
On our searches for , we compared the choices of with those of (computed on dimensions to 8), of (computed, as in [36], on dimensions 2 to 8), and of the approach of Steele and Vigna (SV: the best among the top 0.1% candidates by ). When some dimensions are structurally bounded, optimizes just the bounded dimension: for MWC128, its choice has the largest , but and , against 0.771 and 0.728 for the choice of . SV avoids the worst choices, but for MWC generators the top 0.1% by contains just the candidates with the best , and the chosen candidate may be mediocre in some other dimension ( for MWC128). often chooses the same candidate as , or an essentially equivalent one, but it can choose candidates that are poor in the structurally bounded dimensions: for GMWC256, its choice has , close to the median value over all candidates, against for .
When no dimension is structurally bounded, as for GMWC128, is a sensible criterion: its choice has a better worst figure of merit than the choice of (0.770 against 0.613), which has instead much better and (0.933 and 0.901, against 0.770 and 0.778). The weights of favor the latter, but the choice between the two candidates is a matter of taste.
7 The search
We want the modulus to be a safe prime, so that the period is , and to be as close as possible to , so that the period and the figure of merit in dimension , which is approximately , are as large as possible. We thus consider multipliers whose upper 8 bits are all ones, that is, . For GMWC generators we have the additional parameter , which must be odd and satisfy , so that the carry fits a single word (see Section 3.7); thus, . Optionally, can be restricted to fewer bits, but for it should be larger than to avoid the structural bound of Proposition 4.2 on (for , the bound of Proposition 4.4 is essentially independent of ). We restricted to 32 bits for GMWC128; for GMWC256 and for , has the maximum allowed size (56 and 24 bits, respectively).
For MWC64/32 and MWC128/32 we enumerate exhaustively all multipliers in . In all other cases, we generate random candidate multipliers using a xoshiro256++ generator [1], setting the upper 8 bits; in the GMWC case, we generate e in the same way, masking it to the desired number of bits and setting the lowest bit; pairs with are discarded. We test whether m and are prime using the Miller–Rabin test of the NTL library [34]. Candidates passing the test are then subjected to the spectral test in dimensions 2 to 8 using LatticeTester [20], which computes the shortest vectors of the dual lattice exactly (in the Euclidean norm) by branch-and-bound after lattice reduction [22]. As a sanity check, we recomputed the figures of merit of all the candidates appearing in our tables using fp111, obtaining identical results to the six significant digits printed by LatticeTester; our tables use the exact values, to avoid rounding twice. We also proved the
Sebastiano Vigna
Table 5. Summary of our searches: base, lag, number of candidates (safe-prime moduli on which the spectral test has been run), maximum number of bits of (GMWC only).
| Generator | b | k | Candidates | Bits of e |
|---|---|---|---|---|
| MWC128 | 1 | 11 074 098 | — | |
| MWC192 | 2 | 7 009 216 | — | |
| MWC256 | 3 | 1 172 795 | — | |
| GMWC128 | 1 | 10 941 185 | 32 | |
| GMWC256 | 3 | 1 317 821 | 56 | |
| MWC64/32 | 1 | 22 982 | — | |
| MWC128/32 | 3 | 5 601 | — | |
| GMWC64/32 | 1 | 10 659 050 | 24 | |
| GMWC128/32 | 3 | 10 340 733 | 24 |
primality of and for the five best candidates of each search (using the APR-CL test of PARI/GP through Sage), so the period of the generators we suggest is exactly .
Table 5 reports, for each search, the number of candidates and the maximum number of bits of .
8 Suggested multipliers
Tables 6, 7, and 8 report the five best candidates by for each of our searches, together with their figures of merit in dimensions to 8 (in lower dimensions figures of merit are essentially fixed, see Table 2); the top candidates should be considered essentially equivalent (see Section 6). For GMWC generators, the constant needed by the implementation can be computed, for instance, by five steps of the Newton iteration starting from .
8.1 Small-state generators
Dynamic languages often represent small integers in a machine word with a few tag bits, so arithmetic is fast only if all intermediate results fit in, say, 59 or 60 bits, and the multiplier must be much smaller than the base. The mwc59 generator of Erlang/OTP [9] uses , , and , so is a 59-bit safe prime; the multiplier was chosen by figures of merit, and then by PractRand, among those in for which is a safe prime. When is much smaller than , the shortest vector in dimension two is : for mwc59, , and is very close to the bound 0.075 of Proposition 4.2, so successive triples lie in planes at distance about , as for MWC64/32. Indeed, the documentation of mwc59 states that its raw output has detectable problems in two- and three-dimensional birthday-spacings and collision tests, and the functions returning outputs apply a small xorshift-based scrambler [9].
8.2 Statistical testing
We tested MWC128, plain and with the scrambler X, and MWC192 with X, using the multipliers of Table 6, with PractRand [8] (version 0.96, expanded test set, extra folding), up to 32 TB of output. No failures were detected. We plan to complete such tests for all generators.
PractRand does not detect the three-dimensional defect of plain MWC128 up to 32 TB of output, whereas a specialized birthday-spacings test detects it using about 3 TB of output (Section 4.2): general-purpose test suites have little power against lattice structure in large state spaces, which is precisely the reason why multipliers should be selected using the spectral test. In a small state space, instead, PractRand detects the lattice structure as well: its birthday-spacings test fails after
Table 6. The five best multipliers by for MWC128, MWC192, and MWC256, with their figures of merit in dimensions to 8.
| MWC128 | ||||||
| 0xff6da236274e252e | ||||||
| 0xff9d709b20cf6d2f | ||||||
| 0xff733b5223b28db3 | ||||||
| 0xff94d7cb4e4860fd | ||||||
| 0xff653dd2f96f56c4 | ||||||
| MWC192 | |||||
| 0xff4e24dd418f5433 | |||||
| 0xff34e6efba450121 | |||||
| 0xff149c8cb7b203b8 | |||||
| 0xffc5fcc4f63c15e7 | |||||
| 0xff52883eb6de17c1 | |||||
| MWC256 | ||||
| 0xff32e7e2bdcb0a33 | ||||
| 0xff0e07044e7aeb95 | ||||
| 0xffa68b2009f3b90f | ||||
| 0xff17f83a1f4af883 | ||||
| 0xff3f195afd6226e4 | ||||
Table 7. The five best pairs by for GMWC128 and GMWC256, with their figures of merit in dimensions to 8.
| GMWC128 | |||||||
| 0xffff0cfb4ed6366fa | 0x68935f57 | ||||||
| 0xffd80a0afb781ae9 | 0xb48a3fab | ||||||
| 0xffd492eb05addaa4 | 0x2d1020d3 | ||||||
| 0xffaec0a9a043fbfa | 0xd01f08a3 | ||||||
| 0xff21354728271635 | 0x907a82ab | ||||||
| GMWC256 | |||||
| 0xff76e600f25ae046 | 0x34dabaf938e45b | ||||
| 0xff2eba2993c1d518 | 0xeb1eb4ffc63e7 | ||||
| 0xff43a827aa5cad3e | 0x8946f0524490a7 | ||||
| 0xff5feedbf607f9c5 | 0x821b4720047a0f | ||||
| 0xff01354317d8c2f4 | 0xd5bd05c69bb347 | ||||
Sebastiano Vigna
Table 8. The five best multipliers (or pairs) by for , with their figures of merit in dimensions to 8.
| MWC64/32 | ||||||
| 0xffff145f8 | 0.0275 | 0.8957 | 0.7406 | 0.8239 | 0.7191 | 0.6889 |
| 0xff111cb4 | 0.0277 | 0.9060 | 0.8239 | 0.7542 | 0.7348 | 0.5805 |
| 0xffdb8e77 | 0.0279 | 0.9325 | 0.7737 | 0.7245 | 0.6627 | 0.6848 |
| 0xff874455 | 0.0269 | 0.9075 | 0.8163 | 0.7375 | 0.6871 | 0.7019 |
| 0xffe09073 | 0.0263 | 0.9098 | 0.8261 | 0.7969 | 0.7158 | 0.5697 |
| MWC128/32 | |||||
| 0xffcccc66 | 0.0233 | 0.1847 | 0.8566 | ||
| 0xff7df6e5 | 0.0233 | 0.1845 | 0.8563 | ||
| 0xff555f96 | 0.0233 | 0.1844 | 0.8558 | ||
| 0xff2512f8 | 0.0232 | 0.1840 | 0.8537 | ||
| 0xffafc94a | 0.0231 | 0.1835 | 0.8508 | ||
| GMWC64/32 | |||||||
| 0xff66966f | 0x651e9f | 0.9514 | 0.8246 | 0.8271 | 0.7840 | 0.7437 | 0.7605 |
| 0xff4bce10 | 0x2ec607 | 0.9437 | 0.8981 | 0.7672 | 0.7629 | 0.7419 | 0.7494 |
| 0xffb684f6 | 0x150f3f | 0.9534 | 0.8867 | 0.8385 | 0.6844 | 0.7200 | 0.6418 |
| 0xff3886d5 | 0x62e507 | 0.9569 | 0.7888 | 0.8551 | 0.7871 | 0.6590 | 0.7160 |
| 0xff0a55d9 | 0x97b8cf | 0.9069 | 0.7951 | 0.8156 | 0.8033 | 0.7973 | 0.7122 |
| GMWC128/32 | ||||||
| 0xff6b46e1 | 0x8e02fb | 0.0442 | 0.1894 | 0.7826 | 0.7635 | |
| 0xff37e8ef | 0x19aa97 | 0.0424 | 0.1927 | 0.7756 | 0.7460 | |
| 0xffbb32ec | 0x19a4c3 | 0.0440 | 0.1864 | 0.7979 | 0.7097 | |
| 0xff4e0157 | 0x231523 | 0.0437 | 0.1928 | 0.7820 | 0.6940 | |
| 0xffb21299 | 0x40303 | 0.0425 | 0.1856 | 0.7740 | 0.7749 | |
8 GB of output both on plain MWC64/32 and on plain GMWC64/32, which has no structural defect (Section 4.2), whereas with X MWC64/32 and GMWC64/32 show no anomalies. Passing PractRand shows only that the generators have no gross defect unrelated to their lattice structure.
8.3 Speed
Table 9 reports the time needed to generate a 64-bit output for our generators with , plain and with X, and for a few other popular generators. We include also GMWC192 (), which was not part of our search, using and (a safe-prime modulus; the speed does not depend on the multipliers). As a baseline, we include an MCG with modulus and a 128-bit multiplier: it has the same state size as MWC128, but each step needs three 64-bit multiplications. AES-CTR encrypts a counter with AES-128 using the dedicated AES-NI instructions of the processor: it encrypts four counter blocks at a time, so that the rounds of different blocks are independent and can be executed in parallel, and each group of blocks yields eight 64-bit outputs.
Table 9. Time in nanoseconds to generate a 64-bit output, plain and with the scrambler X (computed on the current state, except for X on GMWC generators, which uses and ), on an AMD EPYC 9R45 CPU (Zen 5) with frequency fixed at 4.5 GHz, compiled by GCC 16.2.0 and Clang 23.1.3 with options -O3 -march=native -fno-unroll-loops (GMWC generators compiled by GCC also with -mno-avx512v1).
| Generator | GCC | Clang | ||
|---|---|---|---|---|
| Plain | X | Plain | X | |
| MWC128 | 0.93 | 0.94 | 0.90 | 0.95 |
| MWC192 | 0.52 | 0.52 | 0.52 | 0.51 |
| MWC256 | 0.52 | 0.53 | 0.50 | 0.53 |
| GMWC128 | 2.07 | 2.02 | 2.05 | 2.04 |
| GMWC192 | 2.05 | 2.04 | 2.04 | 2.05 |
| GMWC256 | 2.05 | 2.03 | 2.05 | 2.04 |
| MCG, | 1.14 | - | 1.16 | - |
| xoroshiro128++ [1] | 0.66 | - | 0.69 | - |
| xoshiro256++ [1] | 0.58 | - | 0.58 | - |
| RomuTrio [32] | 0.58 | - | 0.59 | - |
| SFC64 [8] | 0.49 | - | 0.66 | - |
| AES-CTR [33] | 0.68 | - | 0.57 | - |
We used the harness of [1]: each generator is called times in a loop that accumulates its outputs (by addition for our generators, as X ends with a xor, and by xor for the others), with loop unrolling disabled, and we report the minimum user time per output over five runs.
As with all benchmarks, the results depend on the hardware, on the compiler, and on the way the outputs are used, but they can be explained by the dependency chains of the code in Figure 1. Since the frequency was fixed at 4.5 GHz, a nanosecond in Table 9 corresponds to 4.5 clock cycles; on a modern superscalar processor, the latency of a full -bit multiplication is three or four cycles, and that of a 128-bit addition one or two cycles.
In MWC128, the multiplication by and the addition of the carry lie on the loop-carried dependency chain, and indeed MWC128 needs about 0.93 ns (4.2 cycles) per output. For , instead, depends on , so the multiplications of consecutive steps are independent and can be executed in parallel, and the only dependency from one step to the next is the propagation of the carry. Thus, in spite of their larger state, MWC192 and MWC256 are almost twice as fast as MWC128: they need about 0.52 ns (2.3 cycles) per output, close to the two cycles needed to propagate the carry (an addition followed by an addition with carry), and they are the fastest generators in Table 9, except for SFC64 compiled by GCC, which, however, has a larger state (256 bits) than MWC192, guarantees only a period of at least , and has no jump function.
In GMWC generators, the dependency chain of the carry contains also the multiplication by that computes and the multiplication of by , which depend on each other and cannot be overlapped by increasing : all GMWC generators need about 2.05 ns (9.2 cycles) per output, so they are about twice as slow as MWC128, and four times as slow as MWC192 and MWC256. With AVX-512, GCC moves the 128-bit intermediate values of the plain generators through vector registers, which doubles their time; hence, the option -mno-avx512v1.
Finally, X adds a single instruction that is not on the loop-carried dependency chain, and its cost is less than . For MWC generators we compute it on the current state; computing it on the new state, which shifts the output sequence by one step, gives similar timings.
Sebastiano Vigna
8.4 Recommendations
In all cases, we suggest using the scrambler X: it costs less than 7% of the time per output, and in our tests it removes from all bits every lattice defect we could detect (Section 5). For , X is necessary: without it, MWC64/32, MWC128/32, and GMWC64/32 fail a three-dimensional birthday-spacings test in less than a minute (Section 4.2), and the same caution applies to outputs taken across parallel streams obtained by jumping, which are related by an MCG with the same small modulus (Section 3.8).
Our suggested generator is MWC192X. MWC192 and MWC256 are the fastest generators in Table 9, except for SFC64 compiled by GCC, and MWC192 has the smaller state, with a period of about and jump functions. Its lacunary triples lie in planes at distance about (Corollary 4.3), like the successive triples of MWC128, whose defect needs about 3 TB of output to be detected (Section 4.2): in our opinion, this makes the defect irrelevant in practice, even without X.
When the quality of the underlying lattice is more important than speed, we suggest GMWC128X, which has no structural limitation in dimensions up to eight (the speed of GMWC generators does not depend on the lag). Its worst projections in Table 4 have planes at distance (three dimensions) and (four dimensions); since the number of points needed by a birthday-spacings test grows as the cube root of the number of cells (footnote 11), at the same relative resolution a test on planes at distance d in dimension t needs a number of points proportional to : about for GMWC128, against for the successive triples of MWC128, that is, about 60 times more. GMWC256 has structural limitations in dimensions five and six (Propositions 4.4 and 4.5), but with hyperplanes at distance and .
For 32-bit outputs, we suggest MWC128X/32, which, like MWC192X, performs independent multiplications, and has a period of about . When registers are scarce, as on GPUs, MWC64X/32 is Thomas’s MWC64X with our multiplier (Thomas’s multiplier ranks 15 741st among our 22 982 candidates by ).
9 Conclusions
The form of the modulus of MWC generators reduces the figure of merit in dimension to the shortest vector of a two-dimensional lattice, and bounds it explicitly. The bound is due to dual vectors with small support, so for every lag the vectors lie in planes at distance about . GMWC generators with lag one avoid this limitation, at least empirically, and those with larger lags push it to a distance of about , at the price of two additional dependent multiplications. The scrambler X removes, in our tests, the defect and every other form of lattice structure we could detect, at the cost of a single instruction. Generators with several nonzero coefficients can avoid the structural limitation [5], but at the cost of additional multiplications, to improve a structure that X scrambles anyway.
Since the degenerate behavior of the figures of merit makes classical criteria unsuitable, we select multipliers by a weighted geometric mean, which is essentially forced by requiring that the ranking depend neither on the normalization of the figures of merit nor on the other candidates; as every criterion based on figures of merit, however, it says nothing about the projections it does not consider. Our suggested generator is MWC192X, with GMWC128X as an alternative when the quality of the underlying lattice is more important than speed, and MWC128X/32 when 32-bit outputs are needed.
Several problems remain open. A theoretical analysis of X would make it possible to compare it with GMWC generators on the same ground. It would be interesting to show that the families of dual vectors of Proposition 4.5 are the only ones yielding nontrivial bounds, which would also explain the apparent absence of a structural limitation on for GMWC generators with lag one; to quantify the amount of output that a birthday-spacings test needs to detect a given distance between hyperplanes, extending the rules of thumb of L'Ecuyer and Simard [24]; and, for generators used without a scrambler, to extend our analysis to MWC generators with several nonzero coefficients [5, 29].
Acknowledgments
The author would like to thank Mark Goresky for a long and very interesting exchange on generalized multiply-with-carry generators, which included the confirmation of the error in the formula for the carry and of the erroneous parameter sets discussed in Sections 2 and 3.6, and a discussion on seeding; Pierre L'Ecuyer for useful pointers to the literature on carry generators; David Blackman for several discussions on MWC generators; and Raimo Niskanen for the collaboration on the Erlang/OTP mwc59 generator.
References
[1] David Blackman and Sebastiano Vigna. 2021. Scrambled Linear Pseudorandom Number Generators. ACM Trans. Math. Softw. 47 (2021), 1–32. Issue 4. doi:10.1145/3460772
[2] J. H. Conway and N. J. A. Sloane. 1999. Sphere Packings, Lattices and Groups (third ed.). Grundlehren der mathematischen Wissenschaften, Vol. 290. Springer, New York. doi:10.1007/978-1-4757-6568-7
[3] Raymond Couture and Pierre L'Ecuyer. 1994. On the lattice structure of certain linear congruential sequences related to AWC/SWB generators. Math. Comp. 62, 206 (1994), 799–808. doi:10.1090/S0025-5718-1994-1220826-X
[4] Raymond Couture and Pierre L'Ecuyer. 1995. Linear recurrences with carry as uniform random number generators. In Proceedings of the 1995 Winter Simulation Conference, C. Alexopoulos, K. Kang, W. R. Lilegdon, and D. Goldsman (Eds.). IEEE, Piscataway, NJ, USA, 263–267. doi:10.1109/WSC.1995.478733
[5] Raymond Couture and Pierre L'Ecuyer. 1997. Distribution properties of multiply-with-carry random number generators. Math. Comp. 66, 218 (1997), 591–607. doi:10.1090/S0025-5718-97-00827-2
[6] R. R. Coveyou and R. D. MacPherson. 1967. Fourier analysis of uniform random number generators. J. ACM 14, 1 (1967), 100–119. doi:10.1145/321371.321379
[7] Claude d'Aspremont and Louis Gevers. 2002. Social Welfare Functionals and Interpersonal Comparability. In Handbook of Social Choice and Welfare, Kenneth J. Arrow, Amartya K. Sen, and Kotaro Suzumura (Eds.). Vol. 1. Elsevier, Amsterdam, Chapter 10, 459–541.
[8] Chris Doty-Humphrey. 2025. PractRand: Practically Random, a C++ library of statistical tests for RNGs. https://pracrand.sourceforge.net/. Version 0.96.
[9] Ericsson AB. 2022. rand: Pseudo random number generation. Erlang/OTP STDLIB Reference Manual, https://www.erlang.org/doc/apps/stdlib/rand.html. The mwc59 generator is available since OTP 25.0 (STDLIB 4.0).
[10] George S. Fishman and Louis R. Moore III. 1986. An exhaustive analysis of multiplicative congruential random number generators with modulus . SIAM J. Sci. Statist. Comput. 7, 1 (1986), 24–45. doi:10.1137/0907002
[11] Philip J. Fleming and John J. Wallace. 1986. How Not to Lie with Statistics: The Correct Way to Summarize Benchmark Results. Commun. ACM 29, 3 (1986), 218–221. doi:10.1145/5666.5673
[12] Mark Goresky and Andrew Klapper. 2003. Efficient multiply-with-carry random number generators with maximal period. ACM Trans. Model. Comput. Simul. 13, 4 (2003), 310–321. doi:10.1145/945511.945514
[13] Mark Goresky and Andrew Klapper. 2012. Algebraic Shift Register Sequences. Cambridge University Press, Cambridge. doi:10.1017/CBO9781139057448
[14] Andrew Klapper and Mark Goresky. 1994. 2-adic shift registers. In Fast Software Encryption, Cambridge Security Workshop (December 1993) (Lecture Notes in Computer Science, Vol. 809), Ross Anderson (Ed.). Springer, Berlin, Heidelberg, 174–178. doi:10.1007/3-540-58108-1_21
[15] Andrew Klapper and Mark Goresky. 1997. Feedback shift registers, 2-adic span, and combiners with memory. Journal of Cryptology 10, 2 (1997), 111–147. doi:10.1007/s001459900024
[16] Andrew Klapper and Jinzhong Xu. 1999. Algebraic feedback shift registers. Theoretical Computer Science 226, 1–2 (1999), 61–92. doi:10.1016/S0304-3975(99)00066-3
[17] Donald E. Knuth. 1998. The Art of Computer Programming, Volume 2: Seminumerical Algorithms (third ed.). Addison-Wesley, Reading, MA, USA.
[18] Pierre L'Ecuyer. 1997. Bad lattice structures for vectors of nonsuccessive values produced by some linear recurrences. INFORMS Journal on Computing 9, 1 (1997), 57–60. doi:10.1287/ijoc.9.1.57
Sebastiano Vigna
[19] Pierre L'Ecuyer. 1999. Tables of linear congruential generators of different sizes and good lattice structure. Math. Comp. 68, 225 (1999), 249–260. doi:10.1090/S0025-5718-99-00996-5
[20] Pierre L'Ecuyer. 2025. Lattice Tester Guide: C++ software tools for measuring the uniformity of integral lattices in the real space. https://www-labs.iro.umontreal.ca/~lecuyer/guides/lattester-guide.pdf. Software: https://github.com/umontreal-simul/latticetester.
[21] Pierre L'Ecuyer, François Blouin, and Raymond Couture. 1993. A search for good multiple recursive random number generators. ACM Trans. Model. Comput. Simul. 3, 2 (1993), 87–98. doi:10.1145/169702.169698
[22] Pierre L'Ecuyer and Raymond Couture. 1997. An implementation of the lattice and spectral tests for multiple recursive linear random number generators. INFORMS Journal on Computing 9, 2 (1997), 206–217. doi:10.1287/ijoc.9.2.206
[23] Pierre L'Ecuyer and Christiane Lemieux. 2000. Variance reduction via lattice rules. Management Science 46, 9 (2000), 1214–1235. doi:10.1287/mnsc.46.9.1214.12231
[24] Pierre L'Ecuyer and Richard Simard. 2001. On the performance of birthday spacings tests with certain families of random number generators. Mathematics and Computers in Simulation 55, 1–3 (2001), 131–137. doi:10.1016/S0378-4754(00)00253-6
[25] Pierre L'Ecuyer and Richard Simard. 2007. TestU01: A C library for empirical testing of random number generators. ACM Trans. Math. Softw. 33, 4, Article 22 (2007), 40 pages. doi:10.1145/1268776.1268777
[26] Pierre L'Ecuyer and Richard Simard. 2014. On the lattice structure of a special class of multiple recursive random number generators. INFORMS Journal on Computing 26, 3 (2014), 449–460. doi:10.1287/ijoc.2013.0576
[27] Pierre L'Ecuyer and Renée Touzin. 2004. On the Deng–Lin random number generators and related methods. Statistics and Computing 14, 1 (2004), 5–9. doi:10.1023/B:STCO.0000009417.88960.81
[28] Martin Lüscher. 1994. A portable high-quality random number generator for lattice field theory simulations. Computer Physics Communications 79, 1 (1994), 100–110. doi:10.1016/0010-4655(94)90232-1
[29] George Marsaglia. 1994. Yet another RNG. Posted to the Usenet newsgroup sci.stat.math. August 1, 1994.
[30] George Marsaglia. 2003. Random number generators. Journal of Modern Applied Statistical Methods 2, 1 (2003), 2–13. doi:10.22237/jmasm/1051747320
[31] George Marsaglia and Arif Zaman. 1991. A new class of random number generators. The Annals of Applied Probability 1, 3 (1991), 462–480. doi:10.1214/aoap/1177005878
[32] Mark A. Overton. 2020. Romu: Fast Nonlinear Pseudo-Random Number Generators Providing High Quality. arXiv:2002.11331. doi:10.48550/arXiv.2002.11331
[33] John K. Salmon, Mark A. Moraes, Ron O. Dror, and David E. Shaw. 2011. Parallel random numbers: as easy as 1, 2, 3. In Proceedings of 2011 International Conference for High Performance Computing, Networking, Storage and Analysis (SC'11). ACM, 1–12. doi:10.1145/2063384.2063405
[34] Victor Shoup. 2019. NTL: A library for doing number theory. http://www.shoup.net/ntl/
[35] Guy L. Steele, Jr., Doug Lea, and Christine H. Flood. 2014. Fast Splittable Pseudorandom Number Generators. In Proceedings of the 2014 ACM International Conference on Object Oriented Programming Systems Languages & Applications (Portland, Oregon, USA) (OOPSLA '14). ACM, New York, NY, USA, 453–472. doi:10.1145/2660193.2660195
[36] Guy L. Steele, Jr. and Sebastiano Vigna. 2022. Computationally easy, spectrally good multipliers for congruential pseudorandom number generators. Software: Practice and Experience 52, 2 (2022), 443–458. doi:10.1002/spe.3030
[37] Shu Tezuka, Pierre L'Ecuyer, and Raymond Couture. 1993. On the lattice structure of the add-with-carry and subtract-with-borrow random number generators. ACM Trans. Model. Comput. Simul. 3, 4 (1993), 315–331. doi:10.1145/159737.159749
[38] Sebastiano Vigna. 2026. coll-birth: Large-scale collision and birthday-spacings tests for pseudorandom number generators. https://crates.io/crates/coll-birth. Rust crate, version 0.3.1.