Estimating an area you can't integrate by throwing darts
Draw a unit circle inscribed in a 2×2 square. The circle's area is π; the square's is 4. Scatter points uniformly at random inside the square and count what fraction land inside the circle — geometrically, that fraction should converge to π/4. Multiply the observed fraction by 4 and you have an estimate of π built entirely from counting, no calculus required. This is the canonical example of Monte Carlo integration: turning a geometric probability into a numeric answer via repeated random sampling, named after the Monte Carlo casino by physicists at Los Alamos in the 1940s who used the same trick to estimate neutron diffusion integrals that had no closed form.
let inside = 0, total = 0;
function samplePoint() {
const x = Math.random() * 2 - 1; // uniform in [-1, 1]
const y = Math.random() * 2 - 1;
total++;
if (x * x + y * y <= 1) inside++; // inside the unit circle?
return 4 * inside / total; // running estimate of pi
}
Why it converges like 1/√n, not faster
Each sample is a Bernoulli trial: it lands inside the circle with probability p = π/4 ≈ 0.7854, or outside with probability 1 − p. The observed hit fraction after n samples is an unbiased estimator of p, and by the Central Limit Theorem its standard error shrinks as σ/√n, where σ² = p(1 − p) is fixed by the geometry. In practice that means the error falls by only about 3.16× (√10) every time you take ten times as many samples — to gain one more correct decimal digit of π, you need roughly 100× more points. This 1/√n rate is the defining, unavoidable signature of plain Monte Carlo integration, and it's why it looks unimpressive next to deterministic series for π (Leibniz, Machin-like formulas, or the Chudnovsky algorithm) that converge exponentially fast in the number of terms.
Where Monte Carlo actually wins
The dartboard method is a terrible way to compute π specifically — any of a dozen classical series beats it after a handful of terms. Its real value shows up in the opposite regime: high-dimensional integrals where deterministic quadrature grids become computationally impossible. A grid-based method that places 100 points per axis needs 100^d evaluations for a d-dimensional integral — at d = 10 that's 10^20 points, hopeless. Monte Carlo's error still shrinks as 1/√n regardless of dimension, so a million random samples give a usable estimate at d = 10 where no reasonable grid could. This is exactly why Monte Carlo methods dominate computational finance (pricing path-dependent derivatives), statistical physics (the Ising model, lattice QCD), and the light-transport integrals inside physically based rendering.
Variance reduction: getting more accuracy per sample
Because the error is governed by σ/√n, and n is expensive, most practical improvements to Monte Carlo methods focus on shrinking σ instead of growing n. Stratified sampling divides the square into a grid and guarantees at least one sample per cell, avoiding the unlucky clumping that pure uniform random sampling occasionally produces. Importance sampling draws more samples where the integrand contributes most (irrelevant for a uniform circle, but critical when the function being integrated is sharply peaked). Quasi-Monte Carlo replaces true randomness with low-discrepancy sequences like Sobol or Halton, which fill space more evenly than i.i.d. random points and can push the convergence rate up to nearly 1/n for smooth integrands — asymptotically far better than 1/√n, at the cost of losing simple statistical error bars.
A sanity check: the standard error at a glance
With p = π/4, σ = √(p(1−p)) ≈ 0.4105, so the standard error of the π-estimate (which is 4× the hit-fraction estimate) after n samples is about 4 × 0.4105 / √n ≈ 1.642/√n. At n = 10,000 that predicts an error around ±0.016 — consistent with what you'll see fluctuating on the live counter above even after tens of thousands of points, and a good demonstration that "more samples" buys accuracy slowly, not for free.
Frequently asked questions
Why does the Monte Carlo pi estimate wobble instead of settling immediately?
Because each point is an independent random Bernoulli trial (inside or outside the circle), the running estimate has genuine statistical variance that shrinks only as 1/sqrt(n). Even after tens of thousands of samples you'll see visible fluctuation in the last one or two digits — that's expected, not a bug.
Is this a good way to actually compute digits of pi?
No — it's a teaching example, not a practical algorithm. Its error shrinks as 1/sqrt(n), so getting one more correct digit needs about 100x more samples. Deterministic series like Machin-like formulas or the Chudnovsky algorithm converge exponentially and are what real high-precision pi computations use.
Why is Monte Carlo integration used in real applications despite converging slowly?
Its 1/sqrt(n) error rate is independent of the number of dimensions, while deterministic grid methods need exponentially more points as dimensions grow. In problems with many dimensions — pricing financial derivatives, simulating particle physics, rendering global illumination — Monte Carlo is often the only tractable option, even though it would lose to a deterministic method on a simple 1D or 2D integral.
Try it live
Everything above runs in your browser — open Monte Carlo Pi Estimation and change the parameters while it is running. Nothing is installed, nothing is uploaded, the whole model lives in one tab.
▶ Open Monte Carlo Pi Estimation simulation