Mysteries of the coin toss
From Bernoulli trials to the arcsine law: Binomial counts, the Law of Large Numbers, long streaks and waiting times in fair coin tossing.
https://morrisonkuhlsen.com/coin-toss-mysteries/
Tossing a coin looks like the simplest example in probability — and yet it trips up almost everyone’s intuition. If the coin is fair, why do 100 tosses almost never yield exactly 50 heads? Why do streaks of 8 or 10 heads in a row appear without the coin being biased? Why is the expected waiting time until 10 heads in a row 2,046 tosses rather than 1,024? And why, in a long coin-tossing game, does one side usually stay ahead almost the entire time?
In this guide, we start from a single toss (a Bernoulli random variable) and walk through the Binomial distribution, the Law of Large Numbers, Chebyshev’s inequality, long runs, waiting times, and the arcsine law — with step-by-step calculations, simulations in Julia, R, and Python, and formal proofs in Lean (provas/Provas/CaraOuCoroa.lean).
1. Probability model of a fair coin
In a single toss, the sample space has two possible outcomes:
\[\Omega = \{H, T\}\]where $H$ stands for heads and $T$ stands for tails.
Saying the coin is fair means assigning the same probability to both faces:
\[P(H) = P(T) = \frac{1}{2} = 0.5.\]For multiple tosses, we add a second assumption: the tosses are independent — the outcome of one toss does not change the probability of the next.
2. Formalizing a coin toss as a Bernoulli random variable
To work with sums and averages, we encode the outcome as a number. For the $i$-th toss, define the indicator random variable:
\[X_i = \begin{cases} 1, & \text{if heads},\\ 0, & \text{if tails}. \end{cases}\]Assuming a fair coin ($p = 1/2$) and independent tosses, $X_1, X_2, \ldots, X_n$ are independent and identically distributed (i.i.d.) Bernoulli random variables with parameter $p = 1/2$:
\[X_i \sim \text{Bernoulli}(p), \qquad p = \frac{1}{2}.\]The expected value and variance of a $\text{Bernoulli}(p)$ variable follow directly from the definition (proved in Lean):
\[\mathbb{E}[X_i] = (1 - p)\cdot 0 + p\cdot 1 = p = \frac{1}{2},\] \[\mathrm{Var}(X_i) = (1 - p)(0 - p)^2 + p(1 - p)^2 = p(1 - p) = \frac{1}{2}\cdot\frac{1}{2} = \frac{1}{4} = 0.25.\]The observed proportion of heads in the first $n$ tosses is the sample mean of these indicators:
\[\hat p_n = \frac{1}{n}\sum_{i=1}^{n} X_i.\]3. Multiple tosses: counting heads and the Binomial distribution
In $n$ independent tosses of a coin with probability $p$ of heads, define the count:
\[X = \sum_{i=1}^{n} X_i = \text{number of heads in } n \text{ tosses}.\]This count follows the Binomial distribution:
\[X \sim \text{Binomial}(n, p), \qquad p = \frac{1}{2}.\]For any integer $k$ between $0$ and $n$, the probability mass function for getting exactly $k$ heads is:
\[P(X = k) = \binom{n}{k} p^k (1 - p)^{n - k},\]where $\binom{n}{k} = \frac{n!}{k!\,(n - k)!}$ is the binomial coefficient (the number of ways to choose which $k$ positions out of $n$ are heads).
When the coin is fair ($p = 1/2$), both powers share the same base $1/2$ and combine (proved in Lean):
\[P(X = k) = \binom{n}{k}\left(\frac{1}{2}\right)^k \left(\frac{1}{2}\right)^{n - k} = \binom{n}{k}\left(\frac{1}{2}\right)^n = \frac{\binom{n}{k}}{2^n}.\]Combinatorial reading: there are $2^n$ equally likely sequences of heads and tails, and exactly $\binom{n}{k}$ of them contain $k$ heads. Summing over all $k$ from $0$ to $n$ gives $\sum_{k=0}^{n} \binom{n}{k}/2^n = 2^n / 2^n = 1$ (proved in Lean).
4. Step-by-step calculations
Example A — $n = 5$: probability of getting exactly $k = 3$ heads
Given: $n = 5$ independent tosses (from the problem), $k = 3$ heads (from the problem), and $p = 1/2$ (fair coin).
Formula: since the tosses are independent and $p = 1/2$, $X \sim \text{Binomial}(5, 1/2)$ and
\[P(X = k) = \frac{\binom{n}{k}}{2^n}.\]Substitution:
\[P(X = 3) = \frac{\binom{5}{3}}{2^5}.\]Calculation:
First, the numerator $\binom{5}{3}$:
\[\binom{5}{3} = \frac{5!}{3!\,(5 - 3)!} = \frac{5!}{3!\,2!}.\]Expanding the factorials:
\[5! = 5 \cdot 4 \cdot 3 \cdot 2 \cdot 1, \qquad 3! = 3 \cdot 2 \cdot 1, \qquad 2! = 2 \cdot 1.\]Dividing numerator and denominator by the common factor $3! = 3 \cdot 2 \cdot 1$:
\[\binom{5}{3} = \frac{5 \cdot 4 \cdot 3!}{3!\,(2 \cdot 1)} = \frac{5 \cdot 4}{2 \cdot 1} = \frac{20}{2} = 10.\]Next, the denominator $2^5$:
\[2^5 = 2 \cdot 2 \cdot 2 \cdot 2 \cdot 2 = 32.\]Combining numerator and denominator and reducing by $2$:
\[P(X = 3) = \frac{10}{32} = \frac{10 \div 2}{32 \div 2} = \frac{5}{16} = 0.3125.\]Result: $P(X = 3) = \frac{5}{16} = 0.3125$ (exact, or 31.25%).
Interpretation: across many independent blocks of 5 tosses, you expect to get exactly 3 heads about 31.25% of the time (just over 3 out of every 10 blocks).
Example B — $n = 3$: probability of “at least one head”
Given: $n = 3$ independent tosses and $p = 1/2$. Let $A = {\text{at least one head}}$.
Instead of adding $P(X = 1) + P(X = 2) + P(X = 3)$, it is shorter to use the complement rule:
\[A^c = \{\text{no heads}\} = \{\text{all tails}\}, \qquad P(A) = 1 - P(A^c).\]Substitution: since the tosses are independent and $P(T) = 1/2$,
\[P(A^c) = \left(\frac{1}{2}\right)^3.\]Calculation:
\[\left(\frac{1}{2}\right)^3 = \frac{1}{2} \cdot \frac{1}{2} \cdot \frac{1}{2} = \frac{1}{8}.\]Subtracting from $1 = \frac{8}{8}$:
\[P(A) = 1 - \frac{1}{8} = \frac{8}{8} - \frac{1}{8} = \frac{7}{8} = 0.875.\]Result: $P(A) = \frac{7}{8} = 0.875$ (exact, or 87.5%).
Interpretation: in 7 out of every 8 sequences of 3 tosses, on average in the long run, at least one head appears; only 1 in 8 produces three tails in a row.
Example C — probability of “all heads” in $n$ tosses
Two arguments lead to the same formula:
- Counting sequences: there are $2^n$ equally likely sequences of length $n$, and only 1 of them is all heads ($HH\ldots H$). Hence $P(\text{all heads}) = 1 / 2^n$.
- By independence: letting $H_i = {\text{head on toss } i}$,
Example with $n = 10$:
\[2^{10} = 2^5 \cdot 2^5 = 32 \cdot 32 = 1{,}024.\]Therefore:
\[P(\text{10 heads in 10 tosses}) = \frac{1}{1{,}024} = 0.0009765625 \approx 0.0009766,\]or about 0.0977%.
Interpretation: a chance of 1 in 1,024. Watch out for a common misreading: “1 in 1,024” is a long-run average, not a guarantee that 1,024 blocks of 10 tosses will produce exactly one all-heads sequence.
5. Why does the frequency approach $1/2$ without sticking to $1/2$?
For $X \sim \text{Binomial}(n, 1/2)$, the mean is $\mathbb{E}[X] = np = n/2$ and the variance is $\mathrm{Var}(X) = np(1 - p) = n/4$. For the sample proportion $\hat p_n = X/n$ (with $n \ge 1$), by linearity of expectation and $\mathrm{Var}(aX) = a^2\mathrm{Var}(X)$ (proved in Lean):
\[\mathbb{E}[\hat p_n] = \mathbb{E}\!\left[\frac{X}{n}\right] = \frac{1}{n}\mathbb{E}[X] = \frac{1}{n}\cdot\frac{n}{2} = \frac{1}{2},\] \[\mathrm{Var}(\hat p_n) = \mathrm{Var}\!\left(\frac{X}{n}\right) = \frac{1}{n^2}\mathrm{Var}(X) = \frac{1}{n^2}\cdot\frac{n}{4} = \frac{1}{4n}.\]Taking the square root gives the standard deviation of $\hat p_n$ (its standard error):
\[\mathrm{sd}(\hat p_n) = \sqrt{\frac{1}{4n}} = \frac{1}{2\sqrt{n}}.\]This formula shows how fast the proportion tightens around $0.5$:
| Tosses ($n$) | $\mathrm{sd}(\hat p_n) = 1/(2\sqrt{n})$ | Typical band ($\pm 1$ standard deviation) |
|---|---|---|
| 10 | 0.1581 | 34.2% to 65.8% |
| 100 | 0.0500 | 45.0% to 55.0% |
| 1,000 | 0.0158 | 48.4% to 51.6% |
| 10,000 | 0.0050 | 49.5% to 50.5% |
Cutting the typical error in half requires multiplying $n$ by 4, not by 2. Notice also that while the proportion $\hat p_n$ concentrates around $0.5$, the absolute gap between the count of heads and $n/2$ actually grows on the scale of $\frac{1}{2}\sqrt{n}$: in 100 tosses it is normal to be 5 heads away from 50; in 10,000 tosses it is normal to be 50 heads away from 5,000 — even though 50 out of 10,000 is only 0.5 percentage points.
6. Law of Large Numbers (LLN)
The Law of Large Numbers formalizes this stabilization of the proportion in two versions:
- Weak LLN (convergence in probability): for any margin $\varepsilon > 0$, the chance that $\hat p_n$ lands more than $\varepsilon$ away from $1/2$ goes to zero as $n$ grows:
- Strong LLN (almost sure convergence): with probability $1$, the numerical sequence $\hat p_n$ itself converges to $1/2$:
What the LLN does not say: it does not say the coin “makes up for” past imbalances. When an early streak of heads occurs, what happens next is dilution, not compensation: future tosses stay 50/50, and the early surplus shrinks as a fraction of the growing denominator $n$.
7. A short blackboard proof of the weak LLN via Chebyshev
Because the $X_i$ are independent and each has variance $1/4$, the variance of their sum is the sum of their variances:
\[\mathrm{Var}\!\left(\sum_{i=1}^{n} X_i\right) = \sum_{i=1}^{n} \mathrm{Var}(X_i) = n \cdot \frac{1}{4} = \frac{n}{4},\]giving $\mathrm{Var}(\hat p_n) = \frac{1}{4n}$ for every $n \ge 1$.
Chebyshev’s inequality holds for any random variable $Y$ with mean $\mu$ and finite variance: for every $\varepsilon > 0$,
\[P(|Y - \mu| \ge \varepsilon) \le \frac{\mathrm{Var}(Y)}{\varepsilon^2}.\]Applying it to $Y = \hat p_n$, whose mean is $\mu = 1/2$:
\[P\!\left(\left|\hat p_n - \frac{1}{2}\right| \ge \varepsilon\right) \le \frac{1}{4n\varepsilon^2} \xrightarrow[n\to\infty]{} 0.\]That already proves the weak LLN — and is formally verified in Lean (chebyshev_moeda_justa and cota_chebyshev_tende_zero in provas/Provas/CaraOuCoroa.lean).
A numerical check on how loose Chebyshev is: take $n = 1{,}000$ and $\varepsilon = 0.05$ (falling outside 45% to 55% heads, i.e., $X \le 450$ or $X \ge 550$). Chebyshev gives:
\[\frac{1}{4 \cdot 1{,}000 \cdot 0.05^2} = \frac{1}{4{,}000 \cdot 0.0025} = \frac{1}{10} = 0.10 \quad (10\%).\]The exact Binomial calculation (in Section 15 below) gives $0.001731$ (0.173%), nearly 58 times smaller. Chebyshev is numerically conservative because it uses almost nothing about the distribution — which is precisely why it yields such a short, general proof.
8. Why does the LLN not prevent long streaks?
Long runs of heads are local events: they depend on a handful of consecutive tosses. The LLN governs a global event: the overall average $\hat p_n$ across all $n$ tosses.
A run of 12 heads in a row can occur inside 10,000 tosses while barely nudging the overall proportion: 12 extra heads shift $\hat p_{10{,}000}$ by only $0.0012$ (0.12 percentage points). Both behaviors coexist cleanly:
- the typical longest run of heads grows slowly, on the scale of $\log_2 n$;
- the typical fluctuation of $\hat p_n$ shrinks on the scale of $1/\sqrt{n}$.
9. Independence and the gambler’s fallacy
Even after five heads in a row, if the coin is fair and tosses are independent, the conditional probability of heads on the next toss is unchanged:
\[P(\text{head next} \mid \text{5 heads in a row before}) = P(\text{head}) = \frac{1}{2}.\]The coin has no memory. Believing that “tails is due now to balance things out” is the gambler’s fallacy. What approaches 50% is not the next toss — it is the long-run proportion, as early deviations are divided by an ever-larger $n$.
10. “Getting exactly 50% heads” is less likely than it sounds
For even $n$ and $X \sim \text{Binomial}(n, 1/2)$, the probability of an exact tie ($X = n/2$) is:
\[P\!\left(X = \frac{n}{2}\right) = \frac{\binom{n}{n/2}}{2^n}.\]By Stirling’s approximation (or the normal approximation at the center):
\[P\!\left(X = \frac{n}{2}\right) \approx \sqrt{\frac{2}{\pi n}}.\]Look at $n = 100$:
- Exact Binomial value: $P(X = 50) = \frac{\binom{100}{50}}{2^{100}} \approx 0.079589$ (7.96%).
- Stirling’s approximation: $\sqrt{\frac{2}{100\pi}} \approx 0.079788$ (7.98%).
- Close to 50 ($45 \le X \le 55$): $P(45 \le X \le 55) \approx 0.7287$ (72.87%).
In 100 tosses, landing within $\pm 5$ heads of 50 happens nearly 3 times out of 4, while hitting exactly 50 happens less than 8% of the time (see the right panel of Figure 1). And as $n$ grows, the exact tie gets rarer: at $n = 1{,}000$, it drops to $2.52\%$.
11. Long runs are expected: the longest streak grows like $\log_2 n$
In $n$ independent fair tosses, there are $n - k + 1$ consecutive windows of length $k$ (for $1 \le k \le n$), possibly overlapping. Each window has probability $1/2^k$ of being all heads. By linearity of expectation — which holds even though the windows overlap (proved in Lean) — the expected number of windows of $k$ heads is exactly:
\[\mathbb{E}[\text{windows of } k \text{ heads}] = \frac{n - k + 1}{2^k} \approx \frac{n}{2^k}.\]When that expectation is around $1$, you start expecting to see a streak of length $k$:
\[\frac{n}{2^k} \approx 1 \;\Longrightarrow\; 2^k \approx n \;\Longrightarrow\; k \approx \log_2 n.\]Because this heuristic ignores overlaps, it helps to check the exact values for the longest run of heads (computed by dynamic programming and discussed in Schilling, 1990):
- $n = 100$ ($\log_2 100 \approx 6.64$): the expected longest run of heads is 5.99, and the probability of seeing at least 6 heads in a row is 54.61% — more than half the time!
- $n = 1{,}000$ ($\log_2 1{,}000 \approx 9.97$): the expected longest run of heads is 9.30; there is a 62.42% chance of seeing at least 9 heads in a row, and a 38.55% chance of seeing at least 10.
- $n = 1{,}000{,}000$ ($\log_2 10^6 \approx 19.93$): longest runs of around 19 to 20 heads are routine.
(If you count the longest run of either heads or tails, the mean increases by roughly 1 — near 7 for $n = 100$ and 10.3 for $n = 1{,}000$.)
Takeaway: seeing 6 heads in a row in 100 tosses — or 10 in 1,000 — is not evidence of a biased coin. What would actually look suspicious is a long sequence without any such run.
12. Probability of 100 heads in a row in 100 tosses
Out of the $2^{100}$ possible sequences of 100 tosses, only one is all heads:
\[P(\text{100 heads in 100 tosses}) = \left(\frac{1}{2}\right)^{100} = \frac{1}{2^{100}}.\]The exact value of $2^{100}$ (checked in Lean) is:
\[2^{100} = 1{,}267{,}650{,}600{,}228{,}229{,}401{,}496{,}703{,}205{,}376,\]so:
\[P = \frac{1}{1{,}267{,}650{,}600{,}228{,}229{,}401{,}496{,}703{,}205{,}376} \approx 7.8886 \times 10^{-31}.\]If you keep tossing a fair coin forever, the probability that 100 consecutive heads eventually appear is $1$ (almost surely). The catch is the waiting time.
13. “Keep trying without restarting”: expected waiting time until $k$ consecutive heads
Let $T_k$ be the number of tosses until a run of $k$ consecutive heads occurs for the first time (without discarding intermediate tosses), and write $\mu_k = \mathbb{E}[T_k]$.
A three-line derivation gives $\mu_k$:
- For $k = 0$, no toss is needed: $\mu_0 = 0$.
- To reach $k$ consecutive heads, you must first reach $k - 1$ consecutive heads for the first time — taking $\mu_{k-1}$ tosses on average.
- On the very next toss ($+1$):
- with probability $1/2$, you get heads and you are done;
- with probability $1/2$, you get tails and the streak resets — you are back at square one and need $\mu_k$ additional tosses on average.
Combining both cases:
\[\mu_k = \mu_{k-1} + 1 + \frac{1}{2}\mu_k.\]Subtracting $\frac{1}{2}\mu_k$ from both sides and multiplying by $2$:
\[\frac{1}{2}\mu_k = \mu_{k-1} + 1 \;\Longrightarrow\; \mu_k = 2\mu_{k-1} + 2.\]Solving by induction from $\mu_0 = 0$ (proved in Lean in tempo_espera_moeda_justa, alongside Feller’s general formula $\frac{1 - p^k}{(1 - p)p^k}$ from Vol. 1, Ch. XIII, §7):
Why around $2 \cdot 2^k$ instead of $2^k$?
Look at $k = 2$ (two heads in a row, $HH$): the formula gives $\mathbb{E}[T_2] = 2^3 - 2 = 6$, not $4$. Why? Because occurrences of $HH$ arrive in overlapping clumps: as soon as you see $HH$, there is a 50% chance the next toss is also $H$, creating an overlapping second occurrence ($HHH$). Because the long-run rate of $HH$ is still $1/4$ per toss, clumping into bursts of average size $2$ forces the gaps before the first clump to be longer. (By contrast, the pattern $HT$ can never overlap with itself — and its expected waiting time is exactly $4$, not $6$.)
Concrete numbers
| Consecutive heads ($k$) | Formula $2^{k+1} - 2$ | Expected tosses |
|---|---|---|
| 1 | $2^2 - 2$ | 2 |
| 2 | $2^3 - 2$ | 6 |
| 3 | $2^4 - 2$ | 14 |
| 5 | $2^6 - 2$ | 62 |
| 10 | $2^{11} - 2$ | 2,046 |
For $k = 100$:
\[\begin{aligned} \mathbb{E}[T_{100}] &= 2^{101} - 2 = 2{,}535{,}301{,}200{,}456{,}458{,}802{,}993{,}406{,}410{,}750 \\ &\approx 2.5353 \times 10^{30}\ \text{tosses}. \end{aligned}\]Converting to years using the Julian year of $365.25$ days ($365.25 \times 24 \times 3{,}600 = 31{,}557{,}600$ seconds):
- at $1$ toss/s: $\dfrac{2^{101} - 2}{31{,}557{,}600} \approx 8.0339 \times 10^{22}$ years;
- at $10$ tosses/s: $\approx 8.0339 \times 10^{21}$ years;
- at $100$ tosses/s: $\approx 8.0339 \times 10^{20}$ years.
14. The arcsine law: staying in the lead is usually extreme
Picture a fair zero-sum game: each head adds $+1$, each tail adds $-1$. The running balance after $t$ tosses is a symmetric random walk:
\[S_t = (\text{number of heads up to } t) - (\text{number of tails up to } t), \qquad S_0 = 0.\]Ask a seemingly innocent question: over $n = 1{,}000$ tosses, what fraction of the time is the running balance $S_t$ on the positive side (more heads than tails)?
Almost everyone guesses “around 50% of the time — the coin is fair.” The actual answer is the exact opposite: the arcsine law (Feller, Vol. 1, Ch. III). To handle ties $S_t = 0$ symmetrically, we follow Feller’s convention: step $t$ counts as being on the positive side if $S_t > 0$ or if the walk reaches $S_t = 0$ coming from $S_{t-1} > 0$.
For large $n$, the fraction $L_n / n$ of steps spent on the positive side follows the arcsine distribution, whose cumulative distribution function is:
\[F(x) = \frac{2}{\pi}\arcsin(\sqrt{x}), \qquad 0 \le x \le 1,\]with a U-shaped density $f(x) = \dfrac{1}{\pi\sqrt{x(1 - x)}}$: highest near $0\%$ and $100\%$, and lowest right in the middle ($50\%$).
The numbers are striking:
- The chance of spending at most 10% or at least 90% of the time ahead is:
- The chance of spending between 40% and 60% of the time ahead is only:
- Even in a short game of $2n = 100$ tosses, by Feller’s exact discrete arcsine law, there is a 15.92% chance that one side spends all 100 tosses in the lead, never once falling behind!
Intuitively, once the walk drifts away from zero (say to $+20$), there is no restoring force pulling it back: it keeps taking $+1$ and $-1$ steps with equal probability, and can wander on the positive side for hundreds of tosses before crossing zero again.
15. Reproducing everything in code (Julia, R, and Python)
Block 1 — Exact probabilities, Chebyshev, and the 50% tie
All three languages perform the exact same calculations and produce identical outputs.
using Distributions, Printf
# Example A: exactly 3 heads in 5 tosses
p_exA = pdf(Binomial(5, 0.5), 3)
# Example B: at least 1 head in 3 tosses (complement rule)
p_exB = 1 - pdf(Binomial(3, 0.5), 0)
# Example C: 10 heads in a row in 10 tosses
p_exC = 0.5^10
# Chebyshev vs exact probability for n = 1000, eps = 0.05 (X <= 450 or X >= 550)
n_cheb, eps = 1000, 0.05
chebyshev_bound = 1 / (4 * n_cheb * eps^2)
dist_1000 = Binomial(n_cheb, 0.5)
chebyshev_exact = cdf(dist_1000, 450) + ccdf(dist_1000, 549)
# Exact tie (X = 50) vs close to 50 (45 <= X <= 55) in n = 100 tosses
dist_100 = Binomial(100, 0.5)
p_tie_exact = pdf(dist_100, 50)
p_tie_stirling = sqrt(2 / (pi * 100))
p_near_50 = cdf(dist_100, 55) - cdf(dist_100, 44)
@printf("Example A — P(X = 3 | n = 5): %.4f\n", p_exA)
@printf("Example B — P(X >= 1 | n = 3): %.4f\n", p_exB)
@printf("Example C — P(X = 10 | n = 10): %.10f\n", p_exC)
@printf("Chebyshev (n = 1000, eps = 0.05): bound <= %.4f | exact = %.6f\n",
chebyshev_bound, chebyshev_exact)
@printf("Exact tie (n = 100, X = 50): exact = %.6f | Stirling = %.6f\n",
p_tie_exact, p_tie_stirling)
@printf("Close to 50 (45 <= X <= 55): %.4f\n", p_near_50)
Example A — P(X = 3 | n = 5): 0.3125 Example B — P(X >= 1 | n = 3): 0.8750 Example C — P(X = 10 | n = 10): 0.0009765625 Chebyshev (n = 1000, eps = 0.05): bound <= 0.1000 | exact = 0.001731 Exact tie (n = 100, X = 50): exact = 0.079589 | Stirling = 0.079788 Close to 50 (45 <= X <= 55): 0.7287
# Example A: exactly 3 heads in 5 tosses
p_exA <- dbinom(3, size = 5, prob = 0.5)
# Example B: at least 1 head in 3 tosses (complement rule)
p_exB <- 1 - dbinom(0, size = 3, prob = 0.5)
# Example C: 10 heads in a row in 10 tosses
p_exC <- 0.5^10
# Chebyshev vs exact probability for n = 1000, eps = 0.05 (X <= 450 or X >= 550)
n_cheb <- 1000
eps <- 0.05
chebyshev_bound <- 1 / (4 * n_cheb * eps^2)
chebyshev_exact <- pbinom(450, size = n_cheb, prob = 0.5) +
pbinom(549, size = n_cheb, prob = 0.5, lower.tail = FALSE)
# Exact tie (X = 50) vs close to 50 (45 <= X <= 55) in n = 100 tosses
p_tie_exact <- dbinom(50, size = 100, prob = 0.5)
p_tie_stirling <- sqrt(2 / (pi * 100))
p_near_50 <- pbinom(55, size = 100, prob = 0.5) - pbinom(44, size = 100, prob = 0.5)
cat(sprintf("Example A — P(X = 3 | n = 5): %.4f\n", p_exA))
cat(sprintf("Example B — P(X >= 1 | n = 3): %.4f\n", p_exB))
cat(sprintf("Example C — P(X = 10 | n = 10): %.10f\n", p_exC))
cat(sprintf("Chebyshev (n = 1000, eps = 0.05): bound <= %.4f | exact = %.6f\n",
chebyshev_bound, chebyshev_exact))
cat(sprintf("Exact tie (n = 100, X = 50): exact = %.6f | Stirling = %.6f\n",
p_tie_exact, p_tie_stirling))
cat(sprintf("Close to 50 (45 <= X <= 55): %.4f\n", p_near_50))
Example A — P(X = 3 | n = 5): 0.3125 Example B — P(X >= 1 | n = 3): 0.8750 Example C — P(X = 10 | n = 10): 0.0009765625 Chebyshev (n = 1000, eps = 0.05): bound <= 0.1000 | exact = 0.001731 Exact tie (n = 100, X = 50): exact = 0.079589 | Stirling = 0.079788 Close to 50 (45 <= X <= 55): 0.7287
import math
from scipy.stats import binom
# Example A: exactly 3 heads in 5 tosses
p_exA = binom.pmf(3, n=5, p=0.5)
# Example B: at least 1 head in 3 tosses (complement rule)
p_exB = 1 - binom.pmf(0, n=3, p=0.5)
# Example C: 10 heads in a row in 10 tosses
p_exC = 0.5**10
# Chebyshev vs exact probability for n = 1000, eps = 0.05 (X <= 450 or X >= 550)
n_cheb, eps = 1000, 0.05
chebyshev_bound = 1 / (4 * n_cheb * eps**2)
chebyshev_exact = binom.cdf(450, n_cheb, 0.5) + binom.sf(549, n_cheb, 0.5)
# Exact tie (X = 50) vs close to 50 (45 <= X <= 55) in n = 100 tosses
p_tie_exact = binom.pmf(50, n=100, p=0.5)
p_tie_stirling = math.sqrt(2 / (math.pi * 100))
p_near_50 = binom.cdf(55, 100, 0.5) - binom.cdf(44, 100, 0.5)
print(f"Example A — P(X = 3 | n = 5): {p_exA:.4f}")
print(f"Example B — P(X >= 1 | n = 3): {p_exB:.4f}")
print(f"Example C — P(X = 10 | n = 10): {p_exC:.10f}")
print(f"Chebyshev (n = 1000, eps = 0.05): bound <= {chebyshev_bound:.4f} | exact = {chebyshev_exact:.6f}")
print(f"Exact tie (n = 100, X = 50): exact = {p_tie_exact:.6f} | Stirling = {p_tie_stirling:.6f}")
print(f"Close to 50 (45 <= X <= 55): {p_near_50:.4f}")
Example A — P(X = 3 | n = 5): 0.3125 Example B — P(X >= 1 | n = 3): 0.8750 Example C — P(X = 10 | n = 10): 0.0009765625 Chebyshev (n = 1000, eps = 0.05): bound <= 0.1000 | exact = 0.001731 Exact tie (n = 100, X = 50): exact = 0.079589 | Stirling = 0.079788 Close to 50 (45 <= X <= 55): 0.7287
Block 2 — Simulation: LLN, longest run, waiting time, and arcsine law
Each language uses its own pseudorandom generator (even with seed 123), so the simulated numbers differ slightly across tabs — while all matching the exact theoretical values within Monte Carlo noise.
using Random, Statistics, Printf
Random.seed!(123)
# 1) Law of Large Numbers: one trajectory of 10,000 tosses
tosses = rand(0:1, 10_000)
println("1) Running proportion of heads (LLN):")
for n in (10, 100, 1_000, 10_000)
p_hat = mean(@view tosses[1:n])
@printf(" n = %5d: p_hat = %.4f (theoretical sd = %.4f)\n",
n, p_hat, 1 / (2 * sqrt(n)))
end
# 2) Longest run of heads in 1,000 tosses (5,000 trials)
function longest_heads_run(v)
best = 0
current = 0
for x in v
current = x == 1 ? current + 1 : 0
if current > best
best = current
end
end
return best
end
longest = [longest_heads_run(rand(0:1, 1_000)) for _ in 1:5_000]
@printf("\n2) Longest run of heads in n = 1,000 (5,000 trials):\n")
@printf(" mean = %.2f (exact = 9.30) | P(>= 10) = %.2f%% (exact = 38.55%%)\n",
mean(longest), 100 * mean(longest .>= 10))
# 3) Waiting time until k = 10 consecutive heads (2,000 trials)
function wait_k_heads(k)
steps = 0
streak = 0
while streak < k
steps += 1
streak = rand(Bool) ? streak + 1 : 0
end
return steps
end
times = [wait_k_heads(10) for _ in 1:2_000]
@printf("\n3) Waiting time until k = 10 consecutive heads (2,000 trials):\n")
@printf(" mean = %.1f tosses (exact = 2046)\n", mean(times))
# 4) Arcsine law: fraction of time on the positive side in 10,000 walks of n = 1,000
n_steps = 1_000
fractions = Vector{Float64}(undef, 10_000)
for r in eachindex(fractions)
s = 0
positive = 0
for _ in 1:n_steps
s_prev = s
s += rand(Bool) ? 1 : -1
# Feller's convention: step is on the positive side if s > 0, or reaches 0 from > 0
if s > 0 || (s == 0 && s_prev > 0)
positive += 1
end
end
fractions[r] = positive / n_steps
end
extremes = 100 * mean((fractions .<= 0.10) .| (fractions .>= 0.90))
middle = 100 * mean((fractions .>= 0.40) .& (fractions .<= 0.60))
@printf("\n4) Arcsine law (10,000 walks of n = 1,000):\n")
@printf(" <= 10%% or >= 90%% of the time ahead: %.2f%% (limit = 40.97%%)\n", extremes)
@printf(" between 40%% and 60%% of the time ahead: %.2f%% (limit = 12.82%%)\n", middle)
1) Running proportion of heads (LLN): n = 10: p_hat = 0.6000 (theoretical sd = 0.1581) n = 100: p_hat = 0.5300 (theoretical sd = 0.0500) n = 1000: p_hat = 0.5580 (theoretical sd = 0.0158) n = 10000: p_hat = 0.5121 (theoretical sd = 0.0050) 2) Longest run of heads in n = 1,000 (5,000 trials): mean = 9.26 (exact = 9.30) | P(>= 10) = 37.42% (exact = 38.55%) 3) Waiting time until k = 10 consecutive heads (2,000 trials): mean = 1990.3 tosses (exact = 2046) 4) Arcsine law (10,000 walks of n = 1,000): <= 10% or >= 90% of the time ahead: 41.54% (limit = 40.97%) between 40% and 60% of the time ahead: 12.53% (limit = 12.82%)
set.seed(123)
# 1) Law of Large Numbers: one trajectory of 10,000 tosses
tosses <- rbinom(10000, size = 1, prob = 0.5)
cat("1) Running proportion of heads (LLN):\n")
for (n in c(10, 100, 1000, 10000)) {
p_hat <- mean(tosses[1:n])
cat(sprintf(" n = %5d: p_hat = %.4f (theoretical sd = %.4f)\n",
n, p_hat, 1 / (2 * sqrt(n))))
}
# 2) Longest run of heads in 1,000 tosses (5,000 trials)
longest_heads_run <- function(v) {
r <- rle(v)
heads <- r$lengths[r$values == 1]
if (length(heads) == 0) 0L else max(heads)
}
longest <- replicate(5000, longest_heads_run(rbinom(1000, 1, 0.5)))
cat("\n2) Longest run of heads in n = 1,000 (5,000 trials):\n")
cat(sprintf(" mean = %.2f (exact = 9.30) | P(>= 10) = %.2f%% (exact = 38.55%%)\n",
mean(longest), 100 * mean(longest >= 10)))
# 3) Waiting time until k = 10 consecutive heads (2,000 trials)
wait_k_heads <- function(k) {
steps <- 0L
streak <- 0L
while (streak < k) {
chunk <- rbinom(2048, 1, 0.5)
for (x in chunk) {
steps <- steps + 1L
streak <- if (x == 1L) streak + 1L else 0L
if (streak == k) return(steps)
}
}
}
times <- replicate(2000, wait_k_heads(10L))
cat("\n3) Waiting time until k = 10 consecutive heads (2,000 trials):\n")
cat(sprintf(" mean = %.1f tosses (exact = 2046)\n", mean(times)))
# 4) Arcsine law: fraction of time on the positive side in 10,000 walks of n = 1,000
n_steps <- 1000L
n_sim <- 10000L
steps <- matrix(sample(c(-1L, 1L), n_steps * n_sim, replace = TRUE),
nrow = n_steps, ncol = n_sim)
s <- apply(steps, 2, cumsum)
s_prev <- rbind(0L, s[-n_steps, , drop = FALSE])
# Feller's convention: step is on the positive side if s > 0, or reaches 0 from > 0
positive <- (s > 0L) | (s == 0L & s_prev > 0L)
fractions <- colMeans(positive)
extremes <- 100 * mean(fractions <= 0.10 | fractions >= 0.90)
middle <- 100 * mean(fractions >= 0.40 & fractions <= 0.60)
cat("\n4) Arcsine law (10,000 walks of n = 1,000):\n")
cat(sprintf(" <= 10%% or >= 90%% of the time ahead: %.2f%% (limit = 40.97%%)\n", extremes))
cat(sprintf(" between 40%% and 60%% of the time ahead: %.2f%% (limit = 12.82%%)\n", middle))
1) Running proportion of heads (LLN): n = 10: p_hat = 0.6000 (theoretical sd = 0.1581) n = 100: p_hat = 0.4700 (theoretical sd = 0.0500) n = 1000: p_hat = 0.4930 (theoretical sd = 0.0158) n = 10000: p_hat = 0.4943 (theoretical sd = 0.0050) 2) Longest run of heads in n = 1,000 (5,000 trials): mean = 9.34 (exact = 9.30) | P(>= 10) = 39.16% (exact = 38.55%) 3) Waiting time until k = 10 consecutive heads (2,000 trials): mean = 2071.4 tosses (exact = 2046) 4) Arcsine law (10,000 walks of n = 1,000): <= 10% or >= 90% of the time ahead: 40.36% (limit = 40.97%) between 40% and 60% of the time ahead: 13.13% (limit = 12.82%)
import numpy as np
rng = np.random.default_rng(123)
# 1) Law of Large Numbers: one trajectory of 10,000 tosses
tosses = rng.integers(0, 2, size=10_000)
print("1) Running proportion of heads (LLN):")
for n in (10, 100, 1_000, 10_000):
p_hat = tosses[:n].mean()
print(f" n = {n:5d}: p_hat = {p_hat:.4f} (theoretical sd = {1 / (2 * np.sqrt(n)):.4f})")
# 2) Longest run of heads in 1,000 tosses (5,000 trials)
def longest_heads_run(v):
# Positions with tails (0) separate consecutive blocks of heads (1)
zeros = np.flatnonzero(np.concatenate(([0], v, [0])) == 0)
return int(np.diff(zeros).max() - 1)
samples = rng.integers(0, 2, size=(5_000, 1_000))
longest = np.array([longest_heads_run(row) for row in samples])
print("\n2) Longest run of heads in n = 1,000 (5,000 trials):")
print(f" mean = {longest.mean():.2f} (exact = 9.30) | P(>= 10) = {100 * (longest >= 10).mean():.2f}% (exact = 38.55%)")
# 3) Waiting time until k = 10 consecutive heads (2,000 trials)
def wait_k_heads(k, rng):
steps = 0
streak = 0
while streak < k:
for x in rng.integers(0, 2, size=2048):
steps += 1
streak = streak + 1 if x == 1 else 0
if streak == k:
return steps
times = np.array([wait_k_heads(10, rng) for _ in range(2_000)])
print("\n3) Waiting time until k = 10 consecutive heads (2,000 trials):")
print(f" mean = {times.mean():.1f} tosses (exact = 2046)")
# 4) Arcsine law: fraction of time on the positive side in 10,000 walks of n = 1,000
n_steps, n_sim = 1_000, 10_000
steps = rng.choice([-1, 1], size=(n_sim, n_steps))
s = np.cumsum(steps, axis=1)
s_prev = np.hstack([np.zeros((n_sim, 1), dtype=int), s[:, :-1]])
# Feller's convention: step is on the positive side if s > 0, or reaches 0 from > 0
positive = (s > 0) | ((s == 0) & (s_prev > 0))
fractions = positive.mean(axis=1)
extremes = 100 * np.mean((fractions <= 0.10) | (fractions >= 0.90))
middle = 100 * np.mean((fractions >= 0.40) & (fractions <= 0.60))
print("\n4) Arcsine law (10,000 walks of n = 1,000):")
print(f" <= 10% or >= 90% of the time ahead: {extremes:.2f}% (limit = 40.97%)")
print(f" between 40% and 60% of the time ahead: {middle:.2f}% (limit = 12.82%)")
1) Running proportion of heads (LLN): n = 10: p_hat = 0.3000 (theoretical sd = 0.1581) n = 100: p_hat = 0.4500 (theoretical sd = 0.0500) n = 1000: p_hat = 0.4770 (theoretical sd = 0.0158) n = 10000: p_hat = 0.4934 (theoretical sd = 0.0050) 2) Longest run of heads in n = 1,000 (5,000 trials): mean = 9.30 (exact = 9.30) | P(>= 10) = 38.56% (exact = 38.55%) 3) Waiting time until k = 10 consecutive heads (2,000 trials): mean = 2011.9 tosses (exact = 2046) 4) Arcsine law (10,000 walks of n = 1,000): <= 10% or >= 90% of the time ahead: 41.99% (limit = 40.97%) between 40% and 60% of the time ahead: 12.82% (limit = 12.82%)
16. What breaks in practice: limits of the model
Everything above relies on two ideal assumptions: exact $p = 1/2$ and independence between tosses. For a physical coin flipped with the thumb and caught in the hand, the model is a very good approximation — but not literally exact:
- Dynamical same-side bias: Diaconis, Holmes, and Montgomery (2007) modeled the mechanics of a flipped coin and showed that precession (slight wobble of the spin axis) makes the coin spend slightly more time in the air with its starting face up, predicting roughly a 51% chance of landing on the same side it started on.
- Empirical evidence: Bartoš et al. (2025) tested that prediction across 350,757 recorded flips by dozens of participants and found 50.8% same-side landings (while the overall heads/tails balance stayed even whenever the starting face varied).
- Broken independence: if a tosser always starts with the same face up or controls the impulse, the i.i.d. $p = 1/2$ model no longer holds. One simple habit removes most of the bias: don’t check which face starts up, or randomize the starting side.
17. Summary
- Each fair toss is a $\text{Bernoulli}(1/2)$ random variable with mean $1/2$ and variance $1/4$; the head count $X$ in $n$ independent tosses is $\text{Binomial}(n, 1/2)$ with $P(X = k) = \binom{n}{k}/2^n$.
- The sample proportion $\hat p_n = X/n$ has mean $1/2$ and standard deviation $1/(2\sqrt{n})$: it stabilizes near $0.5$ by the Law of Large Numbers (proved in a few lines via Chebyshev), while an exact tie $X = n/2$ becomes rarer as $n$ grows ($\approx \sqrt{2/(\pi n)}$; 7.96% for $n = 100$).
- Long runs are normal and contradict neither independence nor the LLN: the longest run of heads in $n$ tosses grows on the scale of $\log_2 n$ (exact mean $5.99$ at $n = 100$ and $9.30$ at $n = 1{,}000$).
- The expected waiting time until $k$ consecutive heads without restarting is $\mathbb{E}[T_k] = 2^{k+1} - 2$ ($2{,}046$ tosses for $k = 10$; roughly $2.54 \times 10^{30}$ for $k = 100$).
- By the arcsine law, in a long random walk, spending almost the whole game ahead or behind ($\le 10\%$ or $\ge 90\%$: $40.97\%$) is more than three times as likely as staying balanced between $40\%$ and $60\%$ ($12.82\%$).
18. References
- Feller, W. (1968). An Introduction to Probability Theory and Its Applications, Vol. 1. 3rd ed. Wiley. (Ch. III: Fluctuations in Coin Tossing and Random Walks, arcsine law; Ch. XIII, §7: Application to the Theory of Success Runs, waiting times for runs.)
- Schilling, M. F. (1990). The Longest Run of Heads. The College Mathematics Journal, 21(3), 196–207. https://doi.org/10.1080/07468342.1990.11973306
- Diaconis, P., Holmes, S., & Montgomery, R. (2007). Dynamical Bias in the Coin Toss. SIAM Review, 49(2), 211–235. https://doi.org/10.1137/S0036144504446436
- Bartoš, F. et al. (2025). Fair Coins Tend to Land on the Same Side They Started: Evidence from 350,757 Flips. Journal of the American Statistical Association. https://doi.org/10.1080/01621459.2025.2516210
Comments