On this page
When a game shuffles a deck or a simulation rolls a die, a computer is faking chance with arithmetic. One of the oldest ways to do it is the linear congruential generator, and working one by hand shows where it breaks.
What is a linear congruential generator?
A linear congruential generator (LCG) produces a sequence of pseudo-random integers from the rule xₙ₊₁ = (a·xₙ + c) mod m, where m is the modulus, a the multiplier, c the increment and x₀ the seed (Wikipedia: Linear congruential generator). The map x → (a·x + c) mod m is the same one used in an affine cipher, which you undo with a modular inverse; an LCG simply applies it again and again.
With c = 0 it is a Lehmer generator; for a prime m its longest cycle, m − 1, needs a to be a primitive root modulo m.
It is pseudo-random: the same seed always gives the same sequence, which must repeat within m steps.
How does an LCG work? A worked example with m = 16
Take m = 16, a = 5, c = 3, seed x₀ = 7: multiply by 5, add 3, keep the remainder on dividing by 16.
x₁ = (5·7 + 3) mod 16 = 38 mod 16 = 6
x₂ = (5·6 + 3) mod 16 = 33 mod 16 = 1
x₃ = (5·1 + 3) mod 16 = 8
Carry on and you get 7, 6, 1, 8, 11, 10, 5, 12, 15, 14, 9, 0, 3, 2, 13, 4, and then 7 again. Every value from 0 to 15 appears once: full period.
Change one number. With a = 3 you get 7, 8, 11, 4, 15, 0, 3, 12 and back to 7, only half the values. With c = 4 (a = 5), the seed 7 maps to (35 + 4) mod 16 = 7, and the generator is stuck for ever.
When does an LCG reach full period?
T. E. Hull and A. R. Dobell's 1962 survey "Random Number Generators" in SIAM Review (doi:10.1137/1004061) gave the rule now called the Hull–Dobell theorem. With c ≠ 0, the period equals m for every seed if and only if three conditions hold (summary of the theorem):
c and m have no common factor other than 1;
a − 1 is divisible by every prime factor of m;
a − 1 is divisible by 4 if m is divisible by 4.
Check the example: 3 is coprime to 16; a − 1 = 4 is divisible by 2, the only prime factor of 16; and 4 divides both 16 and a − 1. All three pass. With a = 3, a − 1 = 2 fails the third test, so the cycle halved. With c = 4, the first test fails.
Why do the low bits of an LCG cycle so quickly?
In the example, outputs go odd, even, odd, even, for ever: the last binary digit repeats every 2 steps. The value mod 4 runs 3, 2, 1, 0, repeating every 4 steps.
When m is a power of 2, the lowest k bits of an LCG form a generator modulo 2ᵏ on their own, so they repeat with a period of at most 2ᵏ, and only the top bit gets the full period (Wikipedia: Linear congruential generator). The practical lesson: to get a number below r, use ⌊r·x/m⌋, which keeps the strong top bits, not x mod r, which keeps the weakest ones.
What went wrong with RANDU?
George Marsaglia's 1968 paper "Random numbers fall mainly in the planes" (PNAS, doi:10.1073/pnas.61.1.25) showed that an LCG's outputs, plotted in groups of n as points in n dimensions, lie on a limited set of parallel planes.
IBM's RANDU, used mainly in the 1960s and 1970s, is the famous casualty: xₙ₊₁ = 65539·xₙ mod 2³¹ (Wikipedia: RANDU). Because 65539 = 2¹⁶ + 3, squaring it gives 2³² + 6·2¹⁶ + 9, and 2³² vanishes mod 2³¹. What is left, 6·2¹⁶ + 9, equals 6·65539 − 9, so xₖ₊₂ = 6xₖ₊₁ − 9xₖ (mod 2³¹) for any three consecutive outputs. Plot consecutive triples in a cube and every point lands on one of just 15 planes, against a theoretical upper bound of 2344 planes for that modulus. A 3D simulation run on RANDU was sampling slices, not space.
What do modern libraries use instead?
Python's random module uses the Mersenne Twister, with a period of 2¹⁹⁹³⁷ − 1; its documentation calls it unsuitable for security and points to the secrets module (Python documentation). NumPy's default bit generator is PCG64 (NumPy documentation).
If modular arithmetic is the shaky step, the Fermi Concept Library gives every concept in mathematics and computing its own lesson. To practise, the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning rather than printing an answer.



