Clever Quest Lab ← Labs

Pseudorandom Number Generators

How computers fake randomness – and how to catch a bad generator.

Any whole number. The same seed always gives the same numbers.

Drag the cube to turn it, or click it and use the arrow keys.Drag with the mouse to turn the cube.Drag with your finger to turn the cube.On the cube: arrow keys turn it, P = planes view, R = reset, Space = spin.

Step by step

Start

Internal state now

Last step

    Runs on your device. Nothing you type or record is uploaded. Keeps working without a connection while the page is open. Works with touch. Saves your generator, seed, LCG values, view and sample sizes on this device.

    Added

    How to use it

    Choose a generator and type a seed, any whole number. Every view draws its numbers from that generator starting at that seed, so the same seed always gives exactly the same picture. The browser’s cryptographic generator is the exception: it cannot be seeded and gives fresh numbers each time. Each output is divided by the size of its range, so every generator produces numbers between 0 and 1. Switch views with the tabs, and use the Step by step panel to watch one number being made at a time.

    Controls

    How it works

    A pseudorandom number generator (PRNG) is a fixed rule that turns its state into the next state: xn+1 = f(xn). Nothing in it is random. A good generator only looks random to someone who does not know the state.

    Middle-square squares the number and keeps the middle digits. With 4 digits there are only 10,000 states, and every seed starts repeating within 111 steps (seed 6239 takes the longest). About a fifth of all seeds fall to 0 and stay there; most end in the loop 6100 → 2100 → 4100 → 8100.

    A linear congruential generator (LCG) computes

    xn+1 = (a · xn + c) mod m

    With c ≠ 0 it visits all m values exactly when c and m share no factor, a − 1 is divisible by every prime factor of m, and by 4 if m is (the Hull–Dobell theorem). RANDU uses a = 65539 = 216 + 3 and m = 231. Squaring a gives xn+2 = 6xn+1 − 9xn (mod 231), so for the scaled outputs

    9xn − 6xn+1 + xn+2 = k,   k a whole number from −5 to 9

    Every triple lies on one of these 15 parallel planes. MINSTD (a = 16807, m = 231 − 1) also puts its triples on parallel planes, but they are about 59 times closer together, far too fine to see; its period is 231 − 2.

    Xorshift32 mixes a 32-bit word with three shifts and XORs and cycles through all 232 − 1 non-zero values. MT19937 keeps 624 words, “twists” them every 624 outputs and tempers each output with shifts and masks; its period is 219937 − 1, and seed 5489 must give 3,499,211,612 first. PCG32 runs a 64-bit LCG and outputs a xorshift of its top bits, rotated by an amount taken from the top 5 bits.

    Each test gives a p-value: how often truly random numbers would look at least this unusual. The uniformity test computes χ2 = Σ(O − E)2/E over 100 equal bins. The runs test counts runs of values above and below ½ and compares them with the expected 2n1n2/n + 1. Serial correlation measures whether a number predicts the next one, and the 3D test is a χ2 over small cubes filled by triples. The π estimate takes the numbers in pairs (x, y) and counts those with x2 + y2 < 1: their share approaches π/4, with a typical error of about 1.64/√n.

    None of these PRNGs are safe for passwords, keys or prize draws. 624 outputs of MT19937 reveal its whole state, and a few outputs are enough to predict an LCG. For security, use the browser’s crypto.getRandomValues, which is built for it.

    Things to try

    Frequently asked questions

    Why “pseudo” random?

    Because the numbers come from a formula. The same seed gives the same sequence, which is useful for repeatable simulations and games.

    RANDU passes three tests. Why is it called bad?

    Each test looks for one kind of pattern. RANDU’s single values and pairs are fine; the flaw lives in triples, so only a test or a picture in three dimensions finds it.

    Why does a good generator sometimes look “suspicious”?

    A p-value below 0.01 happens about once in 100 runs even with perfect random numbers. Change the seed: a real flaw keeps failing, a fluke does not.

    Which generator should I use?

    For simulations, a modern one such as PCG32 or MT19937. For anything secret, only a cryptographic generator.

    Sources