Thinking with chance and data
Flipping coins, dropping needles to find pi, a positive test result, and a dot that wanders at random. Behind four different experiments lie the law of large numbers and the square root.
The four works here are all “simplified teaching models” that run random experiments inside the computer. They do not copy a real phenomenon or test exactly; they make the ideas visible.
Coin Flips and the Law of Large Numbers (the site’s “Coin Flips and the Law of Large Numbers”)
Flip a coin many times and the share of heads settles toward 0.5. This is the law of large numbers. Curiously, the gap between heads and tails does not settle. It tends to grow the more you flip. The share approaches 0.5 because the number of flips grows faster than the gap does.
This site’s program checked the following (coin-flips/engine.test.ts).
- With a fixed random seed and 100,000 flips, the share of heads stayed within 0.01 of 0.5.
- With 100 times more flips, the typical distance of the share from 0.5 shrinks by about ten times.
- The typical size of the gap between heads and tails after n flips is about the square root of 2n divided by pi, so with 100 times more flips the gap widens about ten times.
So the wobble of the share shrinks like the square root of the number of flips, while the gap grows like it. It is not true that “after a run of heads, tails is due”. The law of large numbers does not make up for past luck; the many later flips pile up and dilute it.
In history, Jacob Bernoulli is said to have proved the law for “either-or” cases such as coin flips. The proof reportedly took him over 20 years and appeared in Ars Conjectandi (The Art of Conjecturing), published in 1713 after his death. He called it his “golden theorem”, and it later became known as Bernoulli’s theorem. The name “law of large numbers” is said to have been used by Poisson in 1837. Cardano is also said to have stated the same idea earlier, without proof. So the safe wording is that Bernoulli first proved the either-or case, not that he discovered the law.
Buffon’s Needle (the site’s “Buffon’s Needle”)
Drop needles at random on a floor ruled with parallel lines. If the needle is no longer than the gap between lines, the chance that it touches a line is 2l divided by pi times t, with l the needle length and t the gap. If the needle is as long as the gap, the chance is 2 divided by pi (about 0.64). If the needle is half the gap, it is 1 divided by pi. Drop n needles and let h of them touch a line; when the needle equals the gap, pi is estimated as 2n divided by h.
This site’s program checked these (buffon-pi/engine.test.ts).
- The probability formula agrees with a separate calculation that counts on a fine grid.
- With 200,000 random drops, the share that touch a line is close to the formula’s value, and the estimate of pi stayed within 0.03 of the true value.
- If no needle touches a line, pi cannot be estimated.
The problem is named after the French naturalist Buffon. It appears in his 1777 Essai d’arithmétique morale, which seems to have been written around 1760. However, Buffon himself is thought not to have used needles to estimate pi. Estimating pi with needles is an example of a method that came to be known later, using random numbers to estimate a quantity (the Monte Carlo method). In 1901 Lazzarini reported getting 355/113, a value very close to pi, from 3,408 tosses. But the result is so good that people doubt the data can be trusted.
Tests and Base Rates (the site’s “Tests and Base Rates”)
The disease and the test in this work are both imaginary. They are not figures for any real disease or test, and this is not medical advice.
Here is the setting. One person in 100 (1%) has the disease. The test says “positive” for 90% of people who have it, and says “negative” for 91% of people who do not. Test 1,000 people and the counts come out as follows.
- Has the disease, tests positive (true positive): 9 people
- Has the disease, tests negative: 1 person
- Does not have it, tests positive: 89 people
- Does not have it, tests negative: 901 people
The people who test positive number 9 + 89 = 98. Of those, 9 really have the disease, about 9% (9 out of 98). Hearing “a 90% test came out positive”, most people assume it is almost certain. But when the disease is rare, the mistaken positives from the large healthy group outnumber the true ones. The lesson of this work is not to ignore how common the disease was to begin with (the base rate).
This site’s program checked this (base-rate/engine.test.ts). With the same test, the rarer the disease, the lower the share of true cases among positives. With a useless test, which gives positive at the same rate to sick and healthy people alike, a positive result leaves the original rate unchanged.
This calculation is called Bayes’ theorem. Thomas Bayes died in 1761, and his friend Richard Price prepared his essay and sent it to the Royal Society. It was read there in 1763 and printed in the Philosophical Transactions (some sources date the publication 1764). Laplace is said to have reached the same result independently in 1774 and summarised it in a book of 1812.
The Drunkard’s Walk (the site’s “The Drunkard’s Walk”)
Many dots each take one step in a random direction at a time, one square per step. Watching how far they get from the start shows something interesting. The typical distance (the square root of the mean squared distance) is about the square root of the number of steps n. Quadruple the steps and the spread roughly doubles.
This site’s program checked it (random-walk/engine.test.ts).
- Listing every possible walk, the mean squared distance is exactly n (checked with 10 steps in one dimension and 6 steps in two).
- In an experiment with 3,000 dots, the spread agreed with the square root of n to within 5% (100 and 400 steps, one and two dimensions).
- Four times the steps gives about twice the spread.
In fact, the gap between heads and tails in coin flipping is a one-dimensional random walk: it counts +1 for heads and −1 for tails. The story of the gap growing like the square root of n in coin flips and the story here are two views of the same thing.
The term “random walk” is said to have spread from a letter that Karl Pearson sent to the journal Nature in 1905, “The Problem of the Random Walk”. In the same year, 1905, Albert Einstein explained the jiggling of pollen grains in water (Brownian motion) as the result of collisions with water molecules, and derived that the mean squared spread grows in proportion to time. The typical displacement therefore grows with the square root of time. The first to observe the jiggling was Robert Brown, in 1827. Einstein did not discover Brownian motion; he explained it with theory. Perrin’s experiments (around 1908 to 1909) are said to have confirmed the prediction.
What the four share
- Square roots keep appearing: the wobble of a share shrinks like one over the square root of n, while gaps and distances grow like it.
- The more repeats, the clearer the overall shape: each trial is random, but piled up they show a pattern.
- Read a number together with the base rate: what a positive result means depends heavily on how common the thing was to begin with.
All four are simplified teaching models. Experiments with a fixed seed give the same result every time. The test example is imaginary and must not be used for medical decisions.
Sources
- Wikipedia — Law of large numbers
- Wikipedia — Buffon’s needle problem
- Encyclopedia of Mathematics — Buffon, Georges-Louis Leclerc
- Gorroochurn & Levin, Journal Electronique d’Histoire des Probabilités et de la Statistique (2013)
- Wikipedia — Bayes’ theorem
- Wikipedia — Thomas Bayes
- Wikipedia — Random walk
- Wikipedia — Brownian motion
- Cogniq Fun — src/lib/games/coin-flips/engine.test.ts(表の割合が0.5に近づくこと、差が √(2n/π) で広がること / share of heads near 0.5, gap growing like sqrt(2n/pi))
- Cogniq Fun — src/lib/games/buffon-pi/engine.test.ts(針が線にかかる確率 2/π、1/π、円周率の推定 / crossing probability 2/pi and 1/pi, estimating pi)
- Cogniq Fun — src/lib/games/base-rate/engine.test.ts(1000人あたりの人数 9・1・89・901 と陽性のうち本当の割合 9/98 / counts 9, 1, 89, 901 and the share 9/98)
- Cogniq Fun — src/lib/games/random-walk/engine.test.ts(全ての歩き方で距離の2乗の平均がちょうど n、√n に比例して広がること / mean squared distance exactly n, spread like sqrt n)
Updated: 2026-10-17