A streak somewhere in the string
Let L be the longest run of either face in n independent fair flips. A string such as HHTTTH has L = 3. A run longer than k counts toward “at least k” because it contains k consecutive identical outcomes. For one specified starting position, the chance of k identical flips is 2 to the power 1 − k. A longer string gives a streak many possible places to start. Nearby starting positions overlap, so multiplying the single-position probability by the number of positions does not give the probability of at least one streak.
Count the strings that stay below k
Write A(m,k) for the number of m-flip strings whose longest run is shorter than k. For a positive threshold k, every string qualifies when m is smaller than k, including the empty string. At m = k, only the all-heads and all-tails strings fail. For each later flip, the exact recurrence is
A(m,k) = 2^m for 0 ≤ m < k
A(k,k) = 2^k − 2
A(m,k) = 2 A(m−1,k) − A(m−k,k) for m > k
The first term in the last line appends either face to an admissible string. A string that first reaches k at its last flip has an admissible prefix of length m − k. Its final k flips must all be the opposite of the prefix’s last face. Each such prefix has exactly one forbidden extension. Subtracting its count removes each first hit once.
The count immediately gives the cumulative distribution, the probability of any particular longest length, and the chance of at least one qualifying run.
P(L ≤ k) = A(n,k+1) / 2^n
P(L = k) = [A(n,k+1) − A(n,k)] / 2^n
P(L ≥ k) = 1 − A(n,k) / 2^n
For n positive, a run of at least one is certain. A run of at least n requires a constant string and has probability 2 to the power 1 − n. Longer runs have probability zero. Schilling gives an equivalent check. Once the first face is known, the indicators of “same as the previous flip” determine the entire sequence. All indicator strings are equally likely. The longest either-face run equals one plus the longest string of repeats in those n − 1 indicators. Schilling, pp. 198–199.
Distribution and threshold probabilities

| Flips | Mean | 2.5th %ile | Median | 97.5th %ile | Simulated mean | Simulated median |
|---|---|---|---|---|---|---|
| 100 | 6.977432 | 4 | 7 | 11 | 6.9979 | 7 |
| 1,000 | 10.298615 | 8 | 10 | 15 | 10.2850 | 10 |
| 10,000 | 13.620468 | 11 | 13 | 18 | 13.6346 | 13 |
| 1,000,000 | 20.264316 | 18 | 20 | 25 | 20.2770 | 20 |
The median and percentile endpoints are the smallest integers reaching 50%, 2.5% or 97.5% cumulative probability. The two outer percentiles give a central model range. Because the length is a whole number, the range can contain more than 95% of strings.
| Flips | ≥ 5 (%) | ≥ 10 (%) | ≥ 15 (%) | ≥ 20 (%) | ≥ 25 (%) | ≥ 30 (%) |
|---|---|---|---|---|---|---|
| 100 | 97.168967 | 8.665904 | 0.265258 | 0.007820 | 0.000229 | 0.000007 |
| 1,000 | >99.999999 | 62.387041 | 2.968458 | 0.093609 | 0.002912 | 0.000091 |
| 10,000 | >99.999999 | 99.994723 | 26.281317 | 0.947458 | 0.029729 | 0.000929 |
| 1,000,000 | >99.999999 | >99.999999 | >99.999999 | 61.467760 | 2.936197 | 0.093086 |
Each row counts both faces. In one million flips, the median longest run is 20. The most likely single length is 19 (23.69%), just ahead of exactly 20 (23.54%). The chance of at least 20 is 61.47% because that also counts every longer streak. The detailed file gives point, cumulative and exceedance probabilities and the corresponding simulation counts for every integer through 50, plus the remaining tail. A displayed probability may round to one. Distribution and simulation counts (CSV).
Numerical precision
All avoidance counts use integer arithmetic. For large n the program reads the n-th coefficient of the generating function (1 − z^k)/(1 − 2z + z^k). Multiplying numerator and denominator by the denominator evaluated at −z leaves an even denominator. Keeping the appropriate odd or even numerator terms halves the requested index. This evaluates the same recurrence without rounding intermediate counts. The displayed probabilities divide the final counts by 2^n. Coefficient algorithm.
The mean of L is the sum of P(L ≥ k) over positive k. We summed through 50. Counting possible run starts gives a bound of n/2^50 flips on the omitted mean, below 0.000000001 even for a million flips. Both ends of this numerical bound round to the same six decimal places.
To evaluate another sample size or threshold, run code/reproduce.sh --query 1000000 20 from the research directory with your chosen n and k. Add --fraction to print the exact probability of at least one run as a ratio of integers.
Simulation and independent calculation
The simulation drew individual random bits from the 64-bit Mersenne Twister with seed 2026112704. It used sample sizes in ascending order, read each word from its least significant bit and discarded unused bits at the end of each string. A lookup table counted runs inside words and joined runs that crossed a word boundary.
| Flips | Strings | Seed | Largest gap (pp) | 95% bound (pp) | Result |
|---|---|---|---|---|---|
| 100 | 20,000 | 2026112704 | 0.5213 | 1.1264 | Inside |
| 1,000 | 20,000 | 2026112704 | 0.3748 | 1.1264 | Inside |
| 10,000 | 20,000 | 2026112704 | 0.4137 | 1.1264 | Inside |
| 1,000,000 | 20,000 | 2026112704 | 0.3296 | 1.1264 | Inside |
A gap of one percentage point means one additional string per hundred up to that threshold. For 20,000 strings, the two-sided Dvoretzky–Kiefer–Wolfowitz bound, with the 5% error allowance divided among the four sizes, is 1.1264 percentage points over all thresholds. The largest measured gap was 0.5213 points. Uniform simulation bound.
| Flips | Exact mean¹ | Chain mean¹ | Exact median | Chain median | Exact ≥ median (%) | Chain ≥ median (%) |
|---|---|---|---|---|---|---|
| 100 | 6.977432 | 6.977432 | 7 | 7 | 54.23368555 | 54.23368555 |
| 1,000 | 10.298615 | 10.298615 | 10 | 10 | 62.38704052 | 62.38704052 |
| 10,000 | 13.620468 | 13.620468 | 13 | 13 | 70.51309929 | 70.51309929 |
| 1,000,000 | 20.264316 | 20.264316 | 20 | 20 | 61.46775973 | 61.46775973 |
The second model keeps a state for each current run length below k. The next flip resets the length to one or extends it by one, each with probability one half. Once the length reaches k, the model records a hit permanently. Raising its transition matrix to the power n − 1 gives the hit probability. The mean columns marked ¹ use the tail bound above. Enumeration of every string up to ten flips also checked both support boundaries and all intervening lengths.