Buffon's Needle Simulator — Estimating Pi by Probability
The classical experiment: drop needles at random on a floor of equally spaced parallel lines and estimate pi from the crossing probability. Change the count, length, spacing and seed to feel how probability and geometry meet.
Parameters
Needle count N
needles
Needle length L
mm
Line spacing d
mm
Random seed
—
Needle length L exceeds line spacing d. Buffon's simple formula only holds for L not greater than d. Please adjust L or d.
Random numbers come from a deterministic Linear Congruential Generator (LCG), so the same seed reproduces the same result exactly.
Results
—
Crossings m
—
Estimated P = m/N
—
Estimated pi
—
Relative error
Parallel-line floor and thrown needles
Red = needles that crossed a line / Blue = needles that did not (up to 200 needles shown as a sample; statistics use all N throws).
Theory & Key Formulas
With line spacing $d$ and needle length $\ell$ ($\ell \le d$), let $y$ be the distance from the needle center to the nearest line and $\theta$ the angle between the needle and the lines, both independent and uniform. The crossing condition is:
$$y \le \tfrac{\ell}{2} \sin\theta$$
Integrating gives the crossing probability:
$$P = \frac{2\ell}{\pi d}$$
With $m$ crossings out of $N$ throws, $m/N \approx P$, so the estimate of pi is:
$$\hat\pi = \frac{2\,\ell\,N}{d\,m}$$
By the law of large numbers $\hat\pi \to \pi$ as $N \to \infty$, with error scaling as $1/\sqrt{N}$.
What is the Buffon's needle simulator?
🙋
You can compute pi by throwing needles? That sounds like a magic trick.
🎓
Roughly speaking, it is a classical experiment proposed by the 18th-century mathematician Buffon. Draw equally spaced parallel lines on the floor and drop a needle of length L truly at random. The probability that the needle crosses a line is $P = 2\ell/(\pi d)$ — pi appears in the denominator. So if you throw N needles and m cross, you can recover $\pi \approx 2\ell N / (d m)$. Hit "Throw again" above. With the default 5,000 needles you typically land near 3.14.
🙋
But why does pi pop out from random angles and positions?
🎓
Here is the trick: when the needle angle $\theta$ is uniform, its vertical projection is $(\ell/2)\sin\theta$. Take the center position $y$ and the angle as independent uniforms and integrate the crossing condition $y \le (\ell/2)\sin\theta$. You end up integrating $\sin\theta$ over a quarter circle, which equals 2. After dividing by the normalizing areas $\pi/2$ and $d/2$, pi lodges in the denominator. It is a beautiful intersection of probability and geometry.
🙋
If I keep increasing N, does the precision keep improving? Why does changing the seed change the result?
🎓
Precision improves, but slowly. The Monte Carlo standard error is proportional to $1/\sqrt{N}$, so you need 100 times more samples to gain one decimal digit. The tool uses a deterministic Linear Congruential Generator (LCG), so the same seed gives exactly the same outcome. Changing the seed picks a different "story of throws", and m drifts a bit — that fluctuation is the sampling variance. In real practice, you also fix the seed whenever you need reproducible results.
🙋
I got a warning that L cannot be larger than d. Why?
🎓
Good question. When $\ell \le d$, each needle crosses at most one line, so counting crossings directly gives $P = 2\ell/(\pi d)$. But once $\ell > d$, a needle can straddle two or more lines at once, and the probability formula becomes the "generalized Buffon formula" with $\arccos$ terms and extra integrals — much more complex. For teaching purposes, $\ell \le d$ is the clean case, so this tool stops there and warns you, instead of silently producing a misleading estimate.
Frequently asked questions
The relative error scales approximately as $1/\sqrt{N}$. With N = 5,000 the error is typically within a few percent; with N = 100,000 it is usually below half a percent. Because a single run also has sampling variance, the practical recipe is to vary the seed and re-run several times to check that the estimate is stable.
The crossing probability is $P = 2\ell/(\pi d)$, so a larger ratio $\ell/d$ means more crossings. For example $\ell = d$ gives P ≈ 0.637 and $\ell = d/2$ gives P ≈ 0.318. More crossings tend to stabilize the estimate, so it is often pedagogically helpful to keep $\ell$ close to $d$. The defaults here use $\ell = 5, d = 10$, giving $P \approx 1/\pi$.
With probability one, no. The crossing count m is an integer, so $\hat\pi = 2\ell N/(d m)$ only takes discrete values and cannot match the irrational $\pi$ exactly. As N grows, however, the spacing between possible values shrinks and the distribution of $\hat\pi$ concentrates around pi — that is the meaning of convergence here.
It matters. This tool uses a Linear Congruential Generator (LCG) for clarity; that is fine for thousands to tens of thousands of throws. For serious Monte Carlo work with millions of samples, the short period and structural bias of an LCG can introduce systematic error and you should switch to modern PRNGs such as Mersenne Twister or PCG. The fact that the LCG reproduces exactly with the same seed is a feature for education and verification.
Real-world applications
The classical entry point to Monte Carlo: Buffon's needle is one of the earliest examples of computing a deterministic constant (pi) by random sampling, and it is still widely used to introduce the core idea of Monte Carlo methods. "Use random samples to estimate a hard integral or constant" is exactly what every modern Monte Carlo algorithm does.
Probabilistic analysis in CAE: When finite element analysis (FEA) is combined with random material properties or dimensions for reliability analysis (MC-FEA), the foundation is the same as Buffon: random sampling followed by probability estimation. Bridges and aircraft structures use thousands to millions of randomized FEA runs to estimate failure probabilities.
Financial engineering and risk: Monte Carlo option pricing and Value-at-Risk (VaR) computations rely on random sampling to estimate unknown quantities. The relationship between sample size and error that you observe with Buffon's needle is directly the relationship used to plan how many simulations a VaR estimate needs.
Rendering (ray tracing): In photo-realistic 3D rendering, each pixel integrates a light transport equation by random sampling of light paths. The way image noise decreases as you add more samples per pixel is exactly the same phenomenon as the way Buffon's needle estimate of pi stabilizes as you throw more needles.
Common pitfalls and cautions
The most common pitfall is to expect monotone convergence with N. In practice the estimate oscillates around pi and can move away temporarily even as N grows. Try varying the seed at fixed N = 5,000: the result wiggles by a few percent. The right Monte Carlo discipline is not to trust a single run, but to look at the distribution across many independent runs.
A second pitfall is to assume that longer needles always give better precision. Increasing $\ell$ does raise the crossing probability and grows m, which stabilizes the count. But once $\ell > d$, Buffon's simple formula $P = 2\ell/(\pi d)$ no longer applies, and you need the generalized formula that handles multi-line crossings. This tool warns and stops in that regime; be wary of online explanations that suggest "just make the needle longer".
The third pitfall is to increase N while keeping the seed fixed and expect that to be the right way to study sampling variability. Deterministic random numbers simply extend the same sequence, so the independence within one run is preserved, but you are still looking at a single sample path. To probe statistical variability properly, run independent experiments with different seeds (1, 2, ..., M) and average the results. The seed slider here exists precisely to let you experience this "swap independent runs" workflow.