Does Randomness Help?

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

Does Randomness Help?

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.

One Random Point

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.

Left(x₁ + y₁)(x₂ + y₂) ⋯ (x₂₀ + y₂₀)
Rightthe same product, with factor 13 changed to (x₁₃ − y₁₃)
Work modulo p =
x₁ = 5,199 y₁ = 8,450 x₂ = 4,594 y₂ = 4,479 x₃ = 1,784 y₃ = 707 x₄ = 220 y₄ = 2,258 x₅ = 9,315 y₅ = 1,748 x₆ = 1,415 y₆ = 3,339 x₇ = 8,892 y₇ = 5,730 x₈ = 2,319 y₈ = 3,847 x₉ = 258 y₉ = 92 x₁₀ = 2,262 y₁₀ = 1,938 x₁₁ = 5,594 y₁₁ = 8,669 x₁₂ = 6,529 y₁₂ = 6,032 x₁₃ = 9,405 y₁₃ = 1,289 x₁₄ = 1,707 y₁₄ = 4,157 x₁₅ = 1,908 y₁₅ = 4,203 x₁₆ = 3,584 y₁₆ = 8,755 x₁₇ = 1,599 y₁₇ = 4,117 x₁₈ = 380 y₁₈ = 7,478 x₁₉ = 2,601 y₁₉ = 6,398 x₂₀ = 4,091 y₂₀ = 3,905
2,097,152
terms to write down if both sides were multiplied out
79
additions and multiplications the test used
20
degree d, read off the trees
0.20%
d/p: the most an agreement can mislead, with p = 10,007
The two sides come to 2,569 and 911. They are certainly different polynomials: this one point is the proof, and anyone can recheck it. Multiplied out, each side has 2²⁰ terms, and half of them differ in sign. The two agree only where some factor is zero.
Running…
These two sides differ, so every agreement counted here is the test being fooled. Modulo 5 that happened at 0 of points; modulo 1,000,003 at 0 of 0. Each lime bar stays at or below its dashed sky line, d/p, apart from sampling noise.

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.

An application: is there a perfect matching?

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.

L1R1L2R2L3R3L4R4L5R5
R1R2R3R4R5L1L2L3L4L5
Work modulo p =
417,455
the determinant, modulo 1,000,003
yes
perfect matching, by exhaustive search
5
partial matchings that search tried
The determinant is 417,455, not zero, so the polynomial is not zero and a perfect matching certainly exists. The test never says which one; the search found the amber edges.

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.

Go deeper:The Schwartz–Zippel Lemma and Identity Testing

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.

Witnesses Everywhere

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.

composite
what N is (3 digits)
…
of bases are witnesses under the Fermat test
…
of bases are witnesses under Miller–Rabin
…
bases counted: every one from 2 to N − 2
561 is composite, and it is easy to catch: among all 558 bases from 2 to N − 2, counting…. A single random base would almost always do. 561 = 3 × 11 × 17, the smallest Carmichael number.

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.

N − 1 = 24 × d, with d = 35
  1. ad263
  2. a2d166
  3. a4d67
  4. a8d1
  5. a16d =1 ← a to the power N − 1, the Fermat test
Base 2: each line is the square of the line above, modulo N. The highlighted value squares to 1 but is neither 1 nor −1. Modulo a prime, 1 has only those two square roots, so N is certainly composite. It even gives a factor away: the greatest common divisor of that value minus 1 and N is 33.
6.3%
Rabin’s bound on the error for a composite: 4 to the power −k
…
runs in which Miller–Rabin said “prime”
…
runs in which the Fermat test said “prime”
…
for 20 Miller–Rabin bases, measured on this device
N is composite, so every “prime” above is an error. Over 0 runs with k = 2, Miller–Rabin erred 0 times, and the Fermat test 0 times. Trial division would need up to about 24 divisions to settle this N: at a billion a second, under a millisecond.

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.

Go deeper:Primality, Twenty-Five Years of Coins, and the Class BPP

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.

Fake Coins

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.

Simulating…
40%
chance that a single run is wrong
0.021
chance that the majority of 101 runs is wrong (exact)
0.13
the Chernoff bound on that chance
3,219
runs that bring the error below 10−30
Each run is wrong 40% of the time, which sounds useless. But the error of the vote falls in a straight line on this log scale, that is, exponentially in k: 3,219 runs push it below 10−30. Repeating a fast algorithm a fixed number of times leaves it fast, which is why the 1/3 in the definition of BPP could be any constant below 1/2.

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.

Doing without the coins

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.

Input: are these two sides the same polynomial?
x + y
against
x + y + (x + 3y − 75)(x + 3y − 331)(x + 3y − 587)(x + 3y − 843)
Mark on the square the outputs of
…
of all coin sequences fool the test (the lemma allows 1.6%)
256 / 256
seeds of the weak generator give the wrong answer
1 / 256
seeds of the better generator give the wrong answer
65,536 → 256
runs of the test: every coin sequence, or every seed
With real coins the test is fooled only on the four rose lines. But consecutive outputs of the weak generator always satisfy x + 3y = 75 modulo 256, which puts all 256 of its points on those lines. The deterministic algorithm built from it answers “identical”, and is wrong. The better generator’s points miss the lines, and its majority is right.
Three statistical tests on 5,120 bytes from each generator
GeneratorLowest bit of its first 48 bytesFrequencyRunsPairsVerdict
Weak generator
low byte of a 16-bit linear congruential generator
0.0+71.5+385.5fails
Better generator
high byte of a 16-bit xorshift
0.0−0.1−1.3passes

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.

Go deeper:Hardness versus Randomness

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.

Key Takeaways

  • A non-zero polynomial of degree d vanishes at a random point of a field of size p with probability at most d/p, so one evaluation tests an identity that would take exponentially long to expand.
  • Miller–Rabin finds a witness among at least three quarters of the bases of any odd composite, even the Carmichael numbers that fool the Fermat test.
  • A majority vote drives the error down exponentially, so the 1/3 in the definition of BPP could be any constant below 1/2.
  • If sufficiently hard problems exist then P = BPP (Impagliazzo and Wigderson, 1997), which is why most researchers believe randomness adds no real power, without being able to prove it.

Spot a mistake or have an idea?

Search