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
- Generator, Seed, New seed: pick the algorithm and its starting value. The small LCG adds boxes for a, c and m and checks the full-period rules.
- Tabs: 2D pairs, 3D triples, Bitmap, Histogram, Cycle, Tests and π estimate. The arrow keys move between tabs.
- 3D triples: drag with the mouse or a finger, or click the cube and use the arrow keys. P jumps to the planes view, R resets it and Space spins.
- Step by step: Next number, Next 10 and Back to seed, with the internal state and the arithmetic of the last step.
- Sizes: every view has its own sample size. Big jobs run in small slices, so the page stays responsive while it tests a million numbers.
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
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
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
- With RANDU on 3D triples, press “Show RANDU’s 15 planes”, then switch to MINSTD without turning the cube.
- Test RANDU with 10,000 and then 100,000 numbers. The first three tests pass; watch the 3D test.
- Choose middle-square with 4 digits and seed 6239, then open Cycle: 107 steps lead into a loop of 4.
- On the small LCG change a from 137 to 139, or c from 187 to 186, and compare the full-period check with the measured cycle.
- Set the Bitmap to “the lowest bit” for RANDU and the small LCG. The last bit of an LCG with m = 2k is anything but random.
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
- Park SK, Miller KW (1988). Random number generators: good ones are hard to find. Communications of the ACM 31(10), 1192–1201. MINSTD, the LCG with a = 16807 and m = 231 − 1.
- Matsumoto M, Nishimura T (1998). Mersenne twister: a 623-dimensionally equidistributed uniform pseudo-random number generator. ACM Transactions on Modeling and Computer Simulation 8(1), 3–30. MT19937 and its period of 219937 − 1.
- O’Neill ME (2014). PCG: a family of simple fast space-efficient statistically good algorithms for random number generation. Technical report HMC-CS-2014-0905, Harvey Mudd College. PCG32, a 64-bit LCG with a rotated xorshift output.