Computation

The planes a recurrence cannot leave

One multiplication and one addition, taken modulo a fixed number, produce a sequence that passes for random one value at a time. Taken two or three at a time it does not, and the reason is a whole-number relation that pins every point onto one of a small family of parallel lines.
14 min read 6 figures Small cases lieDecided by exhaustion

Worth reading first: Numbers that wrap · What two points can build.

The operation set is two arithmetic steps: multiply by a fixed number, add another, and take the remainder on division by a third. Iterate. The question is the one this field always asks, and the answer is unexpectedly sharp.

256 consecutive pairs from xₙ₊₁ = 137xₙ + 187 mod 256. Consecutive outputs of a linear congruential generator plotted as points of a square, falling on a small family of evenly spaced parallel lines.
Fig. 1 Two hundred and fifty-six consecutive outputs, plotted as the pairs each value makes with the next. Every one of them lies on one of twenty-one evenly spaced parallel lines, and the relation that puts them there was found by searching short whole-number vectors rather than by looking at the picture.

One value at a time, the sequence is unimpeachable: it visits every one of the two hundred and fifty-six residues exactly once before repeating, so any test that counts how often each value appears finds a perfectly flat answer — flatter, in fact, than genuine randomness would produce, which leaves gaps and repeats. Two values at a time, the structure is unmistakable.

Where the lines come from

The recurrence is xn+1axn+c(modm)x_{n+1} \equiv a x_n + c \pmod m, and everything follows from it being linear.

Suppose whole numbers q0q_0 and q1q_1 can be found with q0+q1a0(modm)q_0 + q_1 a \equiv 0 \pmod m. Then for every nn,

q0xn+q1(xn+1c)(q0+q1a)xn0(modm),q_0 x_n + q_1 (x_{n+1} - c) \equiv (q_0 + q_1 a) x_n \equiv 0 \pmod m,

so the quantity on the left is an exact multiple of mm at every step of the sequence. That is the equation of a family of parallel lines, spaced by mm along the direction qq, and every point of the sequence lies on one of them.

There is always such a vector — take q1=1q_1 = 1 and q0=aq_0 = -a — and the interesting question is how short one can be made. A short vector means widely spaced lines, few of them, and an obvious lattice; a long one means the lines are so close together and so numerous that the points look scattered. Finding the shortest such vector is the spectral test, and it is what the figures perform.

Two multipliers, 21 lines against 5. Consecutive output pairs of two linear congruential generators that differ only in their multiplier, one spread over many lines and one collapsed onto few.
Fig. 2 Two multipliers, the same increment and the same modulus, the same two hundred and fifty-six points each. Both visit every value exactly once; one spreads them over twenty-one lines and the other over five.

The contrast is the whole argument. A generator with a small multiplier has a very short relation — for a multiplier of five, the vector (5,1)(-5, 1) works immediately — and the points collapse onto a handful of lines. Nothing about the sequence’s one-at-a-time behaviour changes; the failure is entirely in the pairs.

Full period says nothing about this

There is a clean criterion for the sequence visiting every residue before repeating, and it is worth having because it is what most people check.

Which generators visit all 16 values. A table of multipliers and increments with the three conditions that decide whether the generator has the full period, beside the period found by running it out.
Fig. 3 Multipliers and increments on a modulus of sixteen, with the three conditions that decide the period beside the period found by running the sequence out. The criterion and the measurement agree in every row, and the figure would refuse to draw if they did not.

The conditions are Hull and Dobell’s: the increment must be coprime to the modulus, the multiplier minus one must be divisible by every prime factor of the modulus, and if four divides the modulus then four must divide the multiplier minus one. Meet all three and the sequence has the full period; miss one and it does not.

That criterion is about divisibility, and the lattice is about the size of a short vector. They are independent. Both multipliers in the comparison above have the full period, and one of them is useless in two dimensions. Passing the period test is a necessary condition that carries no information about the structure the sequence has.

Three at a time

Two dimensions is generous. The relation exists in every dimension, and in three it is the classic disaster.

1500 triples from a generator, on 15 planes. Consecutive triples of outputs of a linear congruential generator plotted in a cube, seen from an angle that shows them lying on a small number of parallel planes.
Fig. 4 Fifteen hundred consecutive triples from a generator that was shipped with mainframe operating systems for a decade, drawn in the unit cube from an angle chosen so that the planes are seen edge on. The relation was found by searching short vectors: nine times one value, minus six times the next, plus the one after, is always a multiple of the modulus.

The generator is RANDU, with multiplier 65539=216+365539 = 2^{16} + 3 and modulus 2312^{31}. Squaring the multiplier gives 232+6216+92^{32} + 6\cdot 2^{16} + 9, and modulo 2312^{31} the first term vanishes, leaving 6216+9=6a96 \cdot 2^{16} + 9 = 6a - 9. So

xn+26xn+19xn(mod231),x_{n+2} \equiv 6x_{n+1} - 9x_n \pmod{2^{31}},

which is a relation with coefficients nine, six and one — extremely short. Every triple of consecutive outputs lies on one of fifteen parallel planes in the unit cube, and any simulation that used three consecutive values as a point in space was sampling from fifteen sheets of paper.

The angle in the figure is not a lucky viewpoint. A plane is seen as a line exactly when the line of sight lies inside it, so the camera is aimed by solving for the direction that puts the relation’s normal vector in the screen — three lines of trigonometry, checked by requiring the rotated normal to have no depth component. From a generic angle the same fifteen hundred points look like a cube full of dots and prove nothing.

The same relation at a different modulus

The structure is not an artefact of the particular numbers, and the quickest way to see that is to change all of them.

600 consecutive pairs from xₙ₊₁ = 1229xₙ + 1 mod 2048. Consecutive outputs of a linear congruential generator plotted as points of a square, falling on a small family of evenly spaced parallel lines.
Fig. 5 A different multiplier, a different increment and a modulus eight times larger, with six hundred of its two thousand and forty-eight points drawn. The lines are finer and there are more of them; there are still lines.

Raising the modulus makes the lattice finer, which makes the lines closer together and more numerous, which makes the picture look more like a scatter. It does not remove the lines, and the count of them is exactly what the spectral test reports.

64 consecutive pairs from xₙ₊₁ = 11xₙ + 1 mod 64. Consecutive outputs of a linear congruential generator plotted as points of a square, falling on a small family of evenly spaced parallel lines.
Fig. 6 The other extreme: sixty-four points from a small modulus with a small multiplier. Here the lattice is not subtle at all, and the whole sequence sits on a handful of lines a reader can count.

Between those two is every generator anybody has ever used. The visibility of the structure is a matter of how large the modulus is relative to how many points are drawn; the presence of it is a matter of the recurrence being linear, and that does not vary.

Why the failure is structural

It is tempting to read this as a badly chosen multiplier, and to conclude that a better one would fix it. A better one improves it and cannot fix it, and the reason is a counting argument.

The full sequence of pairs, over one period, is mm points in a grid of m2m^2 cells — a fraction 1/m1/m of the available positions. Those points form a lattice, because the recurrence is linear, and a lattice of mm points in the unit square has a shortest vector whose length is bounded: no lattice can be more evenly spread than a triangular one, and that bound puts a ceiling on how many lines the points can be spread over.

So the number of lines is bounded above by something like m\sqrt{m}, no matter what the multiplier is, and the number of planes in three dimensions by something like m1/3m^{1/3}. For a modulus of 2312^{31} that is about two thousand planes at the very best, and RANDU achieved fifteen. Choosing the multiplier well buys two orders of magnitude and does not change the shape of the answer.

The bound is a statement about lattices rather than about generators, and it is the same kind of statement that decides which integers are sums of two squares: both count lattice points against an area, and both are settled by the shortest vector rather than by anything about the numbers that produced the lattice.

That is the honest statement of what this operation set can and cannot build. It can build a sequence with a long period and a flat distribution of single values. It cannot build one whose consecutive tuples avoid lying on a bounded number of hyperplanes, because linearity forces a lattice and a lattice has a shortest vector.

What the period criterion is really about

The three conditions deserve a paragraph of their own, because the reason they work is a piece of arithmetic worth having and it is not obvious from the statement.

The sequence visits every residue exactly when the map xax+cx \mapsto ax + c is a single cycle on all mm residues. Suppose first that mm is a power of a prime pp. Then cc must be coprime to mm, or the sequence never leaves the multiples of pp; and the multiplier condition says a1a \equiv 1 modulo pp, which makes the map a translation-like shift at the top level and forces it to work its way through the residues rather than settling into a shorter orbit.

The extra clause about four is the exception that always turns up when the prime is two. Modulo four, a1(mod2)a \equiv 1 \pmod 2 leaves two possibilities, and one of them — a3a \equiv 3 — gives a map whose square is the identity plus a shift, halving the period. Requiring a1(mod4)a \equiv 1 \pmod 4 rules it out.

For a general modulus the sequence decomposes into its prime-power parts by the Chinese remainder theorem, and the full period is the product of the parts’ periods only when they are pairwise coprime — which is why the criterion is stated prime by prime.

None of that touches the lattice. The criterion is a statement about the orbit of the map and the lattice is a statement about its linearity, and a map can have a perfect orbit and a terrible linear structure. The figure that draws both is drawing two independent facts about the same three numbers.

Where it fails, and what it needs

The exact arithmetic matters. Every claim above is that a certain quantity is exactly a multiple of the modulus, and the figures keep the products inside the range where whole-number arithmetic is exact, checking that they do rather than assuming it. A generator whose products overflow silently is a different generator.

The period is the count of states, not a measure of quality. A modulus of 2312^{31} gives a period of about two billion, which sounds ample and is the wrong quantity: the sequence of pairs has the same period, and those two billion pairs sit on a few tens of thousands of lines. The number worth quoting is the spacing of the lattice, not the length of the walk. This is an instance of a habit worth resisting generally — quoting the size of a state space when the question is about the shape of what fills it.

A short vector is found by search, and the search has a bound. The figures look for vectors with entries up to a fixed size; a generator whose shortest relation had larger entries would be reported as having no relation within the range searched, which is a statement about the search. For the moduli drawn here the shortest vectors are small and the bound is comfortable.

The increment shifts the lattice and does not change it. Adding cc at each step translates the whole family of lines and leaves their number and spacing alone, which is why the relation above is stated for the differences. A generator with a non-zero increment and one with zero have the same lattice structure and different offsets.

And the lines are a property of the whole period. The figures draw a full period, so the lattice is complete. A short run from a long-period generator shows a scattering of points on a subset of the lines, and the structure is present without being visible — which is the situation everybody who has ever used one of these has actually been in.

Where it came from

Lehmer proposed the multiplicative version in 1949, as a way of producing numbers for computation on machines with very little memory: the whole state is one number, and the step is one multiplication. It is hard to overstate how well suited that is to a machine of the time, and the method is still the fastest thing available if the state must be a single word.

The lattice structure was found by Marsaglia in 1968, in a three-page paper titled Random numbers fall mainly in the planes, and the effect on the field was immediate — the paper is one of the few in the subject that changed practice within a year. RANDU had by then been in use for a decade, and an unknown quantity of published simulation work had sampled three-dimensional points from fifteen planes.

The spectral test itself, as the standard way of grading a generator, is Coveyou and Macpherson’s from 1967, refined by Knuth. It is worth noticing what kind of test it is: not a statistical test run on output, but an exact computation on the multiplier and modulus, performed before any number is generated. That is the difference between asking whether a sequence looks random and asking what the operation set can build. The first question is answered by a test that can only ever fail to reject; the second is answered by exact arithmetic on three numbers, and settles the matter before a single output exists. It is the same distinction the constructibility arguments rest on — a search that finds nothing is evidence, and a degree computation is a proof.

What the pictures cannot show

The two-dimensional figures use a modulus of two hundred and fifty-six so that a full period fits on a page as visible dots. Real generators have moduli in the billions, their lattices are correspondingly fine, and the same structure is invisible at any drawable scale — which is exactly why it went unnoticed for twenty years.

The three-dimensional figure is drawn from the one angle that reveals the planes, and says so. From every other angle the picture is a cube of dots, and a reader who rotated it would conclude there was nothing to see.

And no figure here demonstrates that a generator is good. Everything above is a way of showing structure, and the absence of structure is not something a picture can establish — the best available statement is that the shortest relation is long, which is a bound found by a bounded search.

The ladder from here

Below: numbers that wrap, where modular arithmetic is set up, and what two points can build, where an operation set’s reach is first asked about. Sideways: every fraction exactly once, another sequence that visits everything and is nothing like random, and how long until every one turns up, which is what a genuinely random sequence would do instead of visiting each value once. Above: lattice reduction, the spectral test in higher dimensions, and the non-linear generators built to escape this.

What is worth carrying away

A sequence can pass every test of a kind and fail comprehensively at the next kind up. Counting how often each value appears is a one-dimensional test; the failure here is two-dimensional and invisible to every one-dimensional test there is.

The reason is worth generalising past this example. A rule that is linear has consequences that survive iteration, because linear relations compose into linear relations. Anything built from a linear rule inherits a lattice, and a lattice is the most structured thing a set of points can be. Escaping that requires a non-linear step, and paying for it — which is the trade every generator written since has had to make.