Flip coins and some problems get dramatically easier. Test a polynomial identity with one random point, find primes by chance, and see why most theorists think the coins are unnecessary
Between finding an answer by cleverness and finding it by exhaustive search there is a third option: flip coins. For a few problems randomness looks like magic. The strange conclusion of forty years of research is that the magic is probably an illusion, and that proving so is tied to proving that hard problems exist.
Pick a pair of expressions and a field size, then press Test to evaluate both sides at one random point. Shrink the field to 5 to see the test fooled, and tap the matrix below to ask the same question about a graph.
Lime: the random values and the measured share of points at which the two sides agree (log scale, with the count above each bar). Dashed sky: the Schwartz–Zippel bound d/p, where d is an upper bound on the degree read off the tree. Root values are emerald when the sides agree and amber when the point is a witness that they differ. Arithmetic is modulo the prime p; the largest field here is p = 1,000,003. Your own expressions may use + − × ^, brackets, whole numbers and one-letter variables with an optional index, such as x1; they are multiplied out to check the answer only when that takes at most 5,000 terms.
Tap cells of the matrix to add or remove edges. Each edge gets its own unknown; one random substitution and one determinant answer the question.
Entry (i, j) of the Edmonds matrix is an unknown xij when left vertex i is joined to right vertex j, and 0 otherwise. Its determinant is a polynomial of degree 5 with one term for each perfect matching, and different matchings give different terms, so nothing cancels. Lime: random values from 1 to p − 1. Amber: a matching found by exhaustive search, shown only as a check. This is a small case, 5 vertices a side; matching in bipartite graphs also has fast algorithms that use no randomness.
Are two expressions the same polynomial? The obvious method multiplies both out and compares coefficients, and that can take exponential time: the product (x1 + y1)(x2 + y2)⋯(xk + yk) has 2k terms. Evaluating an expression at a point, by contrast, costs one arithmetic operation per node of its tree.
The Schwartz–Zippel lemma (Jacob Schwartz, 1980; Richard Zippel, 1979; Richard DeMillo and Richard Lipton, 1978) says that a polynomial which is not identically zero, of total degree d, vanishes at a uniformly random point of Sn with probability at most d/|S|. Apply it to the difference of the two sides. If they differ, a random point exposes the difference except with probability d/|S|; if they are the same, every point agrees. So the test has one-sided error: “different” is always right, and “identical” is wrong with small probability. The demo works in the field of integers modulo a prime p, and S is the whole field.
The matching test is the same idea in disguise. The determinant of the Edmonds matrix is a polynomial with one term per perfect matching, so it is non-zero exactly when a matching exists, and a single numeric determinant at random values decides which. The demo is a small case, and the exhaustive search beside it is only there to check the answer.
Polynomial identity testing is the outstanding natural problem with a fast randomised algorithm and no known deterministic polynomial-time one. Valentine Kabanets and Russell Impagliazzo showed in 2003 that this is no accident of neglect: a deterministic polynomial-time identity test would imply circuit lower bounds that nobody knows how to prove. Removing the coins and proving that hard functions exist turn out to be linked, which is the theme of the third demo.
Choose a number and see what every base says about it under two tests. Try 561, then tap a cell to follow its chain of squares, and slide k to see how fast the error shrinks.
One cell for each base a = 2, 3, 4, … in reading order (the first 400). The left half of a cell is the Fermat test, the right half the Miller–Rabin test. Amber: this base is a witness, a proof that N is composite. Pale sky: this base says “probably prime”. Tap a cell to see its computation below. Every base is counted when N is at most 2,000,000; for larger N only the bases drawn are.
Each run draws k fresh bases uniformly from 2 to N − 2. The measured counts come from 2,000 runs (2,000 for N below 67 million, 300 above, to keep the page responsive), so they are estimates. The bound 4−k is a theorem about Miller–Rabin for every odd composite N; the Fermat test has no such bound, which is what Carmichael numbers show.
Fermat’s little theorem says that if N is prime then aN−1 leaves remainder 1 on division by N, for every a not divisible by N. A base a that breaks this rule proves N composite without revealing any factor. The trouble is the Carmichael numbers, such as 561 = 3 × 11 × 17: composites for which every base coprime to N obeys the rule. The Miller–Rabin test repairs this by looking inside the computation. Write N − 1 = 2sd with d odd and square ad repeatedly. Modulo a prime the only square roots of 1 are 1 and −1, so a chain that arrives at 1 from anything else exposes a composite.
Gary Miller gave a deterministic polynomial-time test in 1976, correct if the generalised Riemann hypothesis is true. Robert Solovay and Volker Strassen (1977) and Michael Rabin (1980) removed the hypothesis by choosing bases at random; Rabin proved that for every odd composite N at least three quarters of the bases are witnesses, so k random bases all miss with probability at most 4−k. These fast tests made the RSA cryptosystem practical, since it needs large primes on demand.
In 2002 Manindra Agrawal, Neeraj Kayal and Nitin Saxena proved that primality can be decided in polynomial time with no randomness and no unproved hypothesis: PRIMES is in P. The coins were not needed after all. Miller–Rabin is still what software actually runs, because it is far faster in practice.
BPP is the class of problems solvable in polynomial time by an algorithm that flips coins and, on every input, errs with probability at most 1/3. The randomised primality tests put primality in BPP, in fact in co-RP, where the error is one-sided: a prime is never rejected. Every problem in P is in BPP, since an algorithm may ignore its coins. Whether BPP is larger than P is open, and how BPP relates to NP is unknown.
Slide k to watch a majority vote crush the error of an unreliable algorithm. Then choose inputs for the identity test, compare real coins with two small generators, and build an input that defeats the better one.
Sky: the exact chance, a binomial tail (a tie counts as wrong, so only odd k are used). Dashed: the Chernoff–Hoeffding bound exp(−2k(p − ½)²), which is always above the truth. Lime: 10,000 simulated votes at each of a few values of k, shown only where errors are common enough to be seen in that many votes. The runs must be independent: fresh coins each time.
The identity test draws two random bytes, x and y. Try every possibility and take the majority, and no coins are needed. Choose an input, then build one against the better generator.
| Generator | Lowest bit of its first 48 bytes | Frequency | Runs | Pairs | Verdict |
|---|---|---|---|---|---|
| Weak generator low byte of a 16-bit linear congruential generator | 0.0 | +71.5 | +385.5 | fails | |
| Better generator high byte of a 16-bit xorshift | 0.0 | −0.1 | −1.3 | passes |
The square has one pixel for each pair of bytes (x across, y up): rose where the test wrongly says “identical”. Lime dots are the 256 points a generator can produce, one per 8-bit seed. In the seed grids, emerald is a right answer and rose a wrong one. The table tests the share of 1 bits, the number of runs in the lowest bit, and how evenly pairs of successive bytes fill a 16 × 16 table, each in standard deviations from what true coins would give, on 5,120 bytes; beyond 4 counts as a failure. “Better” means only better against these three tests and these inputs. Both generators are toys with 8-bit seeds, and neither is proved to fool every efficient algorithm; no generator is, without an unproved hardness assumption. The test’s arithmetic is modulo 1,000,003.
A randomised algorithm that reads r random bits can always be made deterministic: run it on all 2r coin sequences and take the majority answer. That costs exponential time in r. A pseudorandom generator stretches a short seed of s bits into r bits that the algorithm cannot tell from random ones, in the sense that its probability of accepting barely changes. Then trying all 2s seeds is enough, and if s is about log r that is only polynomially many runs.
The demo is a toy version. Its test reads 16 random bits, its generators have 8-bit seeds, and both generators can be defeated; with so few seeds any generator can. A real construction must fool every efficient algorithm at once, on every input, and nobody can prove unconditionally that such a generator exists.
What is known is conditional. Noam Nisan and Avi Wigderson (1994) and Russell Impagliazzo and Wigderson (1997) proved that if some problem solvable in time 2O(n) requires circuits of size 2Ω(n), then pseudorandom generators exist that are good enough to give P = BPP. A strong enough lower bound would remove the need for randomness. In the other direction, the theorem of Kabanets and Impagliazzo says that derandomising identity testing would imply lower bounds. Hardness and pseudorandomness are two faces of one question.
Most researchers therefore believe that P = BPP. It is a belief, not a theorem, and it rests on the same kind of unproved hardness as the belief that P ≠ NP. Randomness remains provably essential in other settings: in cryptography, where a key must be unpredictable, and in interactive proofs, where a verifier without coins can check nothing beyond NP. See also: Proofs as Conversations. For circuit lower bounds, see also: Circuits and Lower Bounds.
Spot a mistake or have an idea?