🇺🇦 Українська

🎯 Monte Carlo π Estimator

Throw random darts at a unit square with an inscribed circle of radius 1. The ratio of hits inside the circle to total darts converges to π/4, so π ≈ 4 × (inside / total). A classic demonstration of random sampling.

π Estimate

actual π = 3.141592653…

Controls

Statistics

Total darts0
Inside circle0
Error
Convergence
Tip: The error shrinks roughly as 1/√N. You need ~4× more darts to halve the error. The bottom chart shows the π estimate converging over time.

About this simulation

This simulation estimates π using the classic Monte Carlo dart-throwing method. Random points are scattered uniformly across a square spanning [-1, 1] in both axes, and each point is tested against the unit circle using the condition x² + y² ≤ 1. Because the circle's area is π and the square's area is 4, the fraction of points landing inside the circle approaches π/4, so π is recovered as 4 × (inside ÷ total). It is a vivid demonstration of how random sampling can compute a deterministic constant.

🔬 What it shows

Points (green inside the circle, red outside) accumulate on the canvas while the running estimate 4 × inside/total is displayed to six decimal places. A live convergence chart plots the estimate against the true value of π, the absolute error is reported, and the theoretical one-sigma spread √(π(4−π)/N) shows how uncertainty narrows as more darts are thrown.

🎮 How to use

The simulation runs automatically. The "Darts / frame" slider (1 to 500) sets how many random points are added each animation frame, controlling the sampling rate. The "Reset" button clears all points and statistics to start a fresh run. Watch the π estimate, total darts, inside count, error and convergence band update in real time.

💡 Did you know?

Monte Carlo methods are named after the Monte Carlo casino and were developed by Stanislaw Ulam and John von Neumann while working on nuclear weapons in the 1940s. The same random-sampling idea now powers everything from financial risk modelling to particle physics and computer graphics.

Frequently asked questions

What is the Monte Carlo method for estimating π?

It estimates π by throwing random points at a square that contains an inscribed circle. Since a point falls inside the circle whenever x² + y² ≤ 1, the proportion of points inside approaches the ratio of the areas, which is π/4. Multiplying that proportion by 4 gives an approximation of π that improves as the number of darts grows.

Why does the estimate equal 4 times inside divided by total?

The square spans [-1, 1] on each axis, giving it an area of 4, while the inscribed unit circle has an area of π. For uniformly random points the chance of landing inside the circle is therefore π/4. Rearranging gives π = 4 × (inside ÷ total), which is exactly the formula the simulation evaluates each frame.

What does the Darts per frame slider control?

It sets how many random points are generated on every animation frame, from 1 up to 500. A higher value throws darts faster, so the total count and the accuracy of the estimate grow more quickly, while a lower value lets you watch the sampling process unfold point by point.

How accurate is the result and how fast does it converge?

The error of a Monte Carlo estimate shrinks roughly as 1/√N, so you need about four times as many darts to halve it. The simulation shows this directly through the convergence band ±√(π(4−π)/N), meaning reaching several correct decimal places can require millions of samples. It is accurate but slow compared with analytic formulas for π.

Is throwing random darts actually a good way to compute π?

As a practical method for high-precision π it is inefficient, because the slow 1/√N convergence makes dedicated series far quicker. Its real value is conceptual: it demonstrates the law of large numbers and the power of Monte Carlo integration, a technique that excels at high-dimensional problems where traditional numerical methods become intractable.