Monte Carlo Simulation (Coding)
Answer a hard quantitative question by simulating it many times with random draws and averaging the results. Easy to code, general-purpose, and the fallback when no formula exists — but its error only shrinks like one over the square root of the sample count.
Prerequisites: Big-O Complexity, The Law of Large Numbers
Monte Carlo simulation is the art of answering a question by playing it out at random, many times, and averaging what happens. When a problem is too tangled for a clean formula — a path-dependent payoff, a messy multi-step game, an integral in twenty dimensions — you can often just simulate it: draw random inputs, run the process, record the outcome, and repeat until the average settles down. The The Law of Large Numbers guarantees that average converges to the true expected value. It's the most general numerical tool there is, and it's often a five-line loop.
The core recipe
To estimate the expected value of some quantity where is random, draw independent samples and average:
Here is your estimate, each is one random scenario, is the outcome you record from that scenario, and is how many times you ran it. That's the whole method. The subtlety is how accurate the estimate is.
The catch: error shrinks like one over root-N
The standard error of the estimate is
where is the standard deviation of a single outcome. The is the whole story: to cut the error in half you need four times the samples; for one more decimal digit of accuracy — a 10× improvement — you need 100× the runs. Monte Carlo is easy to write and slow to make precise, and every serious use of it is really about reducing so you need fewer samples.
Estimate = average of many random trials. The error falls only as , so accuracy is expensive: 100× the samples buys one extra digit. This is why variance-reduction tricks, not raw sample count, are where the real work goes.
Worked example: estimating pi by throwing darts
The classic coding-interview Monte Carlo is estimating . Throw darts uniformly at a square and count how many land inside the quarter-circle of radius 1 centered at a corner. The quarter-circle has area and the square has area , so the fraction of darts inside is about . Multiply by 4 and you have an estimate of .
import random
def estimate_pi(n):
inside = 0
for _ in range(n):
x, y = random.random(), random.random() # uniform in the unit square
if x*x + y*y <= 1.0: # inside the quarter-circle?
inside += 1
return 4 * inside / n
With n = 10_000 you'll typically land near 3.14; with n = 1_000 it wobbles in the low 3.1s. Watch the convergence: going from 1,000 to 100,000 samples (100×) only steadies roughly one more digit — the law in action.
Where it shows up in quant-dev interviews
Monte Carlo questions are everywhere in quant hiring because the desk lives on them. "Estimate ," "estimate the expected number of coin flips until three heads in a row," and "simulate a random walk and report the probability it stays positive" are pure coding-interview versions. The finance version is pricing by simulation: simulate thousands of terminal prices under the risk-neutral dynamics, average the discounted payoff, and you have a price for an option too path-dependent for a closed form — an Asian, a barrier, a basket. The interviewer is checking three things: can you set up the estimator, do you know the error is , and can you name a way to shrink it.
The professional move is variance reduction — get the same accuracy with fewer samples by lowering . Antithetic variates pair each draw with its mirror image so errors partly cancel; control variates lean on a related quantity you know exactly; and Importance Sampling re-aims your draws at the region that matters (essential for rare-event tails). Mentioning even one of these signals you've done real Monte Carlo work.
Two traps. First, always set the random seed when you need reproducible results — a simulation you can't rerun to the same numbers is a debugging nightmare. Second, respect independence: a poor or repeated random-number generator, or reusing draws across trials, secretly correlates your samples, and correlated samples break the error bound — your estimate looks confident while being biased.
The mindset to carry: when there's no formula, simulate. Set up the estimator, run enough samples for the accuracy you need, quote the error honestly as , and reach for variance reduction before reaching for a bigger loop. It's the numerical backbone behind Monte Carlo Integration and risk-scenario engines alike.
Related concepts
Practice in interviews
Further reading
- Glasserman, Monte Carlo Methods in Financial Engineering
- Robert & Casella, Monte Carlo Statistical Methods