Randomness and RNG
You don't have to trust a generator — you can test it. We cover the tests you can run in a browser, the heavyweight batteries the professionals use, and the crucial limits of what a pass means.
A random number generator makes a checkable claim: every outcome equally likely, no memory, no pattern. This article is a working guide to checking it — from a five-minute browser test to the batteries used in formal certification — and, just as importantly, to what a passing grade does not prove.
The chi-square goodness-of-fit test, documented in the NIST/SEMATECH e-Handbook of Statistical Methods, asks: are the observed counts consistent with the expected ones?
Worked example. Generate 4,500 single picks from 1–45. A fair generator expects each number 4,500/45 = 100 times. Compute:
χ² = Σ (observed − expected)² / expected, summed over the 45 numbers.
With 45 categories there are 44 degrees of freedom, and the standard critical value at the 5% significance level is 60.48. If your χ² lands below 60.48, the counts are consistent with uniformity; far above it, something is skewed.
This test has teeth. Take the modulo-biased generator, where numbers 1–31 have probability 6/256 and 32–45 have 5/256. Its expected χ² contribution per draw is small — but it accumulates linearly with sample size, while chance fluctuation only grows with the square root. At 4,500 draws the biased generator's expected χ² is already ≈ 44 (fair baseline) + 4,500 × Σ(pᵢ − 1/45)²/(1/45) ≈ 44 + 55 ≈ 99 — comfortably past the 60.48 line, so the bias is usually caught with one afternoon's data. Our randomness tester runs exactly this test on pasted or generated numbers and shows the full working; the draw simulator will happily manufacture test batches.
Chi-square only counts frequencies; a generator that emitted 1, 2, 3, …, 45, 1, 2, 3, … forever would pass it perfectly. So you add tests that see sequence:
A generator worth trusting should be passing frequency, runs and KS simultaneously, on fresh data, repeatedly.
Serious validation stacks hundreds of tests, each hunting a different pattern family:
These are what testing labs actually run when certifying gambling RNGs — the certification side is covered in certified RNGs and testing labs.
Here is the crucial, frequently-fumbled distinction.
Passing proves: the output is statistically indistinguishable from uniform, independent draws — no frequency skew, no serial correlation, no structure of any kind the battery knows how to look for. A generator with modulo bias or a sticky pattern will be caught fast.
Passing cannot prove:
Math.random() uses xorshift128+, which passes TestU01 — yet published work recovers its full internal state from a handful of outputs and predicts everything that follows. Statistical tests interrogate the output's shape; predictability is about whether the mechanism can be reverse-engineered. No battery of shape-tests can see that. Only design analysis can — which is why the trustworthy stack is a CSPRNG by construction plus passing tests, never tests alone.Testing is how you catch broken generators. Understanding why a pass isn't a security proof is how you avoid being fooled by intact-looking ones.
Last verified: 2026-08-29