In this article
Before you start
Why interviewers ask this
Candidates who can write the loop but cannot say how wrong the answer is tend to lose the point on the follow-up. The task is a compact test of three skills: turning a geometry fact into a probability, writing a short simulation, and stating the size of the error. A common form is "read this code that estimates pi and explain your findings", often next to questions on sampling inside a circle and on unbiased results from a biased coin.
A weak answer says "random points, count, multiply by four". A strong answer also says why the factor is four, what the estimate's spread is at the number of points used, and why this is a poor way to get many digits of pi but a good template for integrals that have no formula.
The Monte Carlo estimate of pi from first principles
In plain words
- Monte Carlo method
- A Monte Carlo method is a way of estimating a number by running many random trials and averaging the outcomes, instead of deriving the number from a formula. The name was chosen at Los Alamos in the 1940s for statistical sampling done on a computer (Metropolis, 1987).
- Pseudo-random number generator
- A pseudo-random number generator is a deterministic program that produces a sequence of numbers that behaves like random draws, and that repeats exactly when it is started from the same seed, the starting number.
Picture a square dartboard with a quarter-circle painted on it. A blindfolded thrower lands every dart somewhere on the square, anywhere equally likely. If, say, 78 of 100 darts (an illustrative count) land in the painted quarter, the painted part is about 78% of the board. You know the board's area, so you can read off the painted area, and that area contains pi.
The analogy breaks in one place. A real thrower aims and clusters. The method only works because the points are spread evenly, so the quality of the random numbers matters.
In precise terms, the share of uniform points in a unit square with averages over many runs. Averaged over many runs, four times the observed share is exactly , which statisticians call an unbiased estimate.
Step 1: Throw random points into a square
Each trial draws two independent uniform numbers, and , both between 0 and 1. The input is a random number generator; the output is a point in the unit square, whose area is one.
In Python, random.random() returns the next floating-point number in the range 0.0 up to but not including 1.0, and the module uses the Mersenne Twister as its core generator (Python 3 random documentation, read 2026-10-24). The same page says that reusing a seed value reproduces the sequence from run to run, which is why a simulation with a fixed seed gives the same answer every time and why its output can be saved and checked.
You now know how to make one point; the next step decides whether it counts.
Step 2: Count the points inside the quarter circle
A point is inside the quarter circle when , which says its distance from the corner at the origin is at most one. The input is the point; the output is a yes or no, and the number of yeses is the count.
The quarter circle of radius one has area , about 0.7854, and the square has area one. So each point is a hit with probability . In a run of one million points with seed 0, 785,610 landed inside, a hit share of 0.78561.
Whether the boundary counts, less than or equal to one, changes nothing, because the circle's edge has zero area and a random point lands on it with probability zero.
Step 3: Turn the hit share into pi
The estimate is four times the hit share. If the share of hits is , then
where is the number of points. The input is the count and ; the output is a number close to three. The factor of four undoes the quarter: the hit probability is , so is four times that.
With seed 0, one million points gave four times 0.785610, which is 3.14244, and that is within 0.00085 of the true value 3.141591. The estimate is unbiased: averaged over many runs it equals pi. It is still random, so every run gives a slightly different number, and the next step says by how much.
Step 4: How wrong can the estimate be?
The standard error is the standard deviation of an estimate across repeated runs. For this estimate it is four times , where a single run uses its hit share in place of . The count of hits is binomial, meaning the number of successes in independent trials with the same success probability , so the share has variance , and multiplying by four multiplies the standard deviation by four. This is the general rule, with the standard deviation of one trial, which the NIST e-Handbook of Statistical Methods (read 2026-10-25) states for the sampling distribution of the mean.
With the standard error is about 1, so a hundred times more points buys only ten times less error.
For 10,000 points the error is 0.01641, so a result of 3.1352 should be reported as 3.14 with an error of about 0.02, not as 3.1352.
Step-by-step description
- Draw x and y uniformly between 0 and 1.
- Keep points with x squared plus y squared at most one.
- Four times the hit share estimates pi.
- The standard error is about 1.64 over root n.
- Each point has two independent uniform coordinates between 0 and 1.
- A point is inside the quarter circle when x squared plus y squared is at most one.
- Four times the hit share estimates pi.
- The standard error is about 1.64 over the square root of n.
Knowing it is half the job. Saying it is the other half.
Interview Clozo is an AI interview copilot for real interview rounds: it hears the question and puts a strong answer on your screen, and you say it in your own words. See the features or how it works.
Start Free Trial →Worked example
| Points (n) | Inside | Estimate | Absolute error | Standard error |
|---|---|---|---|---|
| 100 | 76 | 3.04000 | 0.10159 | 0.17083 |
| 1,000 | 782 | 3.12800 | 0.01359 | 0.05223 |
| 10,000 | 7,838 | 3.13520 | 0.00639 | 0.01647 |
| 100,000 | 78,711 | 3.14844 | 0.00685 | 0.00518 |
| 1,000,000 | 785,610 | 3.14244 | 0.00085 | 0.00164 |
The input is one seeded run per row using the loop of Steps 1 to 3, where the standard error is the formula of Step 4 applied to that run's hit share1. The estimate wanders and the absolute error does not fall every time: at 100,000 points the error of 0.00685 is larger than at 10,000 points, 0.00639, and both errors are within about 1.3 standard errors (0.0165 and 0.0052). The rows share seed 0, so each larger run extends the smaller one and the rows are not independent. A single run is one draw from a spread, not a trend line.
To see the spread directly, the script repeats the experiment with 200 different seeds per size. The standard deviation of the 200 estimates was 0.1689 at 100 points, 0.0512 at 1,000 points and 0.0175 at 10,000 points, against theory values of 0.1642, 0.0519 and 0.01641.
Text description of the diagram
- Correct digits, minus log10 of the standard error, are 0.78 at 100 points and 2.78 at 1,000,000 points.
- Each hundredfold increase in points adds one correct digit; the standard error falls to a tenth.
Each hundredfold increase in points cuts the typical error to a tenth, which is one more correct digit in the chart above: the theoretical standard error is 0.16422 at 100 points and 0.00164 at 1,000,000 points1. The cost of accuracy follows by rearranging the formula. For a standard error of 0.1 you need about 270 points, for 0.01 about 26,968, and for 0.001 about 2,696,7671.
Text description of the diagram
- A standard error of 0.1 needs about 270 points.
- A standard error of 0.01 needs about 26,968 points.
- A standard error of 0.001 needs about 2,696,767 points.
Trade-offs and edge cases
Monte Carlo is a slow way to compute pi and a good way to compute things that have no formula. The method earns its place when the quantity is an average over a shape or a process that cannot be integrated by hand, because the error is and the rate of does not depend on the number of dimensions (, the spread of one trial, depends on the problem). A regular grid with 10 points per axis needs 10 points in one dimension, 100 in two and 1,000,000 in six1, so grids become unusable where random sampling still works.
Several edge cases come up in follow-ups.
- The generator is not random. Python's generator is deterministic and the documentation calls it completely unsuitable for cryptographic purposes (Python 3 random documentation, read 2026-10-24). For simulation that is a feature, because a fixed seed makes a result reproducible. Report the seed.
- Variance can be cut. Art Owen's Monte Carlo book lists chapters on variance reduction, importance sampling and quasi-Monte Carlo, which are the standard ways to get a smaller error from the same number of trials (Owen, Monte Carlo theory, methods and examples, contents page (2023)). Variance reduction rearranges the trials to cut spread; importance sampling draws more where the quantity matters most; quasi-Monte Carlo uses low-discrepancy sequences, points that cover the space evenly without forming a grid. Name them without deriving them unless asked.
- The answer is a range. An estimate without a standard error is incomplete. For a normal distribution, about 95.45% of the probability lies within two standard deviations of the centre1, so the estimate plus or minus two standard errors is a roughly 95% interval, though the normal shape is only an approximation for small . In the 200-seed runs at 100, 1,000 and 10,000 points, 565 of 600 estimates (94.2%) landed within two theory standard errors of pi1. For a bound that needs no normal shape, see Chebyshev's inequality.
How to answer it in an interview
Write a simulation that estimates pi and tell me how accurate it is.
I'd draw n points with x and y uniform between zero and one and count how many satisfy x squared plus y squared at most one. That share estimates pi over four, so I multiply by four. The estimate is unbiased, but it is random. The hit count is binomial, so the standard error is four times the square root of p times one minus p over n, about 1.64 over root n. A hundred times more points gives only ten times less error, so a million points gets me within a few thousandths. It is a poor way to get many digits but a good template for integrals without a formula.
The spoken structure is: set up the area ratio, give the estimator, then state the error and its square-root cost.
Common mistakes
- "Reuse one random number for both coordinates" (wrong):
x = random(); y = xputs every point on the diagonal, so the hit share is , about 0.7071 and the estimate is about 2.83 however many points you use (a one-million-point run gave 2.82951). Draw and separately (right). - "Divide the count by with integer division" (wrong): in C, Java or Python 2,
inside / nis 0 whenever inside is less than , so the estimate is 0; the same count, 785,610 of 1,000,000, gives 0 under floor division1. Convert to a float first (right). - "Ten times the points gives ten times the accuracy" (wrong). The error scales as one over the square root of , so ten times the points gives about three times less error (right).
- "My result of 3.1352 is correct to four decimals" (wrong). With 10,000 points the standard error is 0.0164, so only the first decimal or two are reliable (right).
- "The estimate is wrong because a second run gave a different number" (wrong). The result is a random variable; different seeds give different values scattered around pi (right).
- "Python's
randomis fine for anything random" (wrong). It is for simulation; use thesecretsmodule for security tokens (right).
Follow-up questions
We want to sample values between [0, 1] uniformly but they should remain within the unit circle. How do you do it?
Use rejection sampling: draw a point uniformly from the enclosing square and keep it only if it lies inside the circle, which is the same test as Step 2. Rejection sampling is a method that generates draws from a target region by sampling a larger easy region and discarding the misses. For a circle in its bounding square, the script needed 1.2708 tries per accepted point against a theory value of 1. The same accept-or-discard idea builds rand10() from rand7(); the rejection sampling page walks through it.
Given a biased coin, how would you generate unbiased outcomes?
Flip the coin twice: output 1 on heads then tails, output 0 on tails then heads, and discard the two equal outcomes and repeat. Heads-tails and tails-heads have the same probability, , for any bias strictly between 0 and 1. With a heads probability of 0.7, the script produced 100,000 bits with 49.98% ones, using about 4.78 flips a bit against a theory value of 4.761.
How many random points do you need for three correct decimals?
Three correct decimals means the estimate is within 0.0005 of pi. For about 95% confidence the half-width must be 1.96 standard errors, the normal-distribution value that leaves 2.5% in each tail1, so the standard error must be 0.0005 divided by 1.96, which is 0.000255, which takes about 41 million points1. That is more than 15 times the 2.7 million that a standard error of 0.001 needs.
Why not use a grid of evenly spaced points instead of random ones?
A grid is fine in one or two dimensions, but its size multiplies with each added dimension, as the numbers above show for ten points per axis. Random points keep an error of in any dimension, and the standard error is available from the sample.
Given code that estimates pi, what do you explain about your findings?
Say what each line does, name the ratio, point out the factor of four, then comment on the seed, the number of points and the standard error. Add one limitation, such as that a single run says nothing about the next, and one improvement, such as reporting an interval or using more points. A Bayes' theorem or t-test question later in the same round tests the same habit of stating uncertainty.
- The quarter circle has area pi over four of the unit square, so four times the hit share estimates pi.
- The estimate is unbiased but random; report a standard error of about 1.64 over the square root of n.
- Error falls as one over root n: a hundred times more points for ten times less error.
- Monte Carlo is a poor way to get digits of pi and a good template for integrals without a formula, in any dimension.
- Fix and report the seed; Python's generator is for simulation, not for security.
Sources
- random: Generate pseudo-random numbers (Python 3 standard library) — Python Software Foundation (undated version page; date = read date), 24 Oct 2026
- The Beginning of the Monte Carlo Method, N. Metropolis, Los Alamos Science, Special Issue — Los Alamos National Laboratory, 1 Jan 1987
- Monte Carlo theory, methods and examples (book in progress, contents page) — Art B. Owen, Stanford University (copyright line 2009 to 2023), 1 Jan 2023
- 1.3.6.6.1. Normal distribution, NIST/SEMATECH e-Handbook of Statistical Methods (page undated; date = read date) — NIST (U.S. National Institute of Standards and Technology), 25 Oct 2026