Monte Carlo Estimation of Pi
GeneratorsEverything runs locally in your browser — nothing is uploaded
The idea in one line: scatter random points in a unit square and the fraction landing inside the quarter circle is about π/4, because the quarter circle has area π/4 while the square has area 1. So `π ≈ 4 × points inside ÷ total points`. The accuracy is only of order 1/√n, and that has to be said plainly. The error falls with the square root of the sample count: each extra decimal place costs a hundred times the samples. A million points settles to roughly three decimals and a hundred million to four — while the digits of pi tool hands you ten thousand decimals directly. So the value here is not in computing pi; it is that this is the smallest runnable example of Monte Carlo, and that it makes visible how one extra decimal costs an extra two orders of magnitude. Why the convergence is shown and not just a number. Given only the final value you cannot tell whether the estimate got lucky or actually converged. Listing the running estimate at powers of ten lets you watch it creep towards 3.14159… and see how much it wobbles. The random source is `crypto.getRandomValues`, not `Math.random`.
Features
- Estimates pi from the fraction of points inside the quarter circle, times four
- Reports the sample count, the points inside, the estimate and the relative error
- Lists convergence at powers of ten, so you can see how slowly the accuracy climbs
- Sample count is adjustable up to two million points (about a second)
- The error is relative, so it can be compared directly against the 1/√n scale
- The random source is `crypto.getRandomValues`, consumed without reuse between draws
- Each run differs slightly; click Run again for a fresh batch of points
- Everything runs on your machine with no requests
How to use
- Set the sample count (a million by default)
- Read the estimate and relative error in the result block
- Scroll to the convergence rows to see what each extra power of ten buys
- Click Run again for a fresh set of random points
FAQ
- Why does the estimate always wobble around 3.14 instead of being exact?
- That is a property of the method itself, not a defect in the implementation. Monte Carlo error falls as 1/√n — a hundredfold increase in samples only divides the error by ten. A million points typically land within about 0.1%, which is roughly three decimals of stability. For an exact pi, use the digits of pi tool, which returns ten thousand built-in decimals.
- What is this tool actually for?
- Two things. First, it is the smallest runnable example of Monte Carlo — the idea is understood in a sentence, and it demonstrates the same “estimate a fixed quantity by random sampling” that underlies financial pricing, physical simulation and Bayesian inference. Second, it makes the slowness of convergence visible: in the table, a thousand points and a million points are not far apart, even though the sample count grew a thousandfold — an intuition that formulas alone do not build.
- How many samples should I use?
- It depends what you want to see. Just the shape of convergence: ten thousand points, instant. An estimate close to 3.14159: a million points takes a couple of hundred milliseconds and usually lands within 0.1%. Two million is the ceiling — beyond it the returns are fixed by the method (the √n term) and the wait grows noticeably.
Related tools
Pi and e Digits
LookupsRead any stretch of pi (10,000 digits) or Euler’s number e (5,000 digits), or find where a digit string first appears. Checked against known values.
Random Number Generator
GeneratorsGenerate random integers or decimals with a chosen range, count and decimal places, plus deduplication, sorting and binary, octal or hex output.
Fibonacci Sequence
GeneratorsGenerate the first N Fibonacci numbers, any single term or a range, or look up which term a number is. BigInt throughout, exact to term 1000 and beyond.
UUID Generator
GeneratorsGenerate a column of UUID v4, v7, ULID, NanoID or MongoDB ObjectId values, one per line, ready to paste into a spreadsheet. Runs in your browser.
Lorem Ipsum
GeneratorsGenerate placeholder text as Latin lorem ipsum, English sentences, Chinese or Japanese filler, in paragraphs, sentences or words — all in your browser.
Mock Data
GeneratorsDescribe your fields once and get realistic mock records, exportable as JSON, CSV, TSV, SQL, XML, YAML or TOML. All generated in your browser.