This Part taught you to derive probabilities: count outcomes, apply rules, reverse conditionals. But many real questions resist a clean formula. The way out is wonderfully direct: build the random experiment in code, run it tens of thousands of times, and let the frequencies answer the question.
The method was developed in the 1940s by Stanislaw Ulam and John von Neumann on the Manhattan Project, while computing how neutrons scatter, a problem too tangled for pen and paper. Ulam's uncle gambled at the Monte Carlo casino in Monaco, and the codename stuck. Today the same idea prices options, renders movie lighting, trains reinforcement-learning agents, and powered the engine behind AlphaGo.
The Monte Carlo Loop
The entire method is one short loop. Define what counts as a success, run the experiment once at random, repeat a large number of times, and divide the successes by the trials. That ratio is your estimated probability.
For example, the chance of at least one six in four rolls is 1 − (5/6)⁴ ≈ 0.5177 by the complement rule. Simulate 500,000 sets of four rolls, count the ones containing a six, and the frequency comes out 0.517: a measurement of the probability, accurate to a few thousandths, with no algebra at all.
A Classic: Estimating π with Darts
The showpiece of Monte Carlo: estimate π using nothing but random points. Scatter darts uniformly over a unit square. The fraction landing inside the quarter circle equals the ratio of the areas, which is π/4. Multiply by 4 and you have π.
With 100,000 darts the estimate comes out near 3.141. No formula for π was used, only random points and a ratio of areas. This is Monte Carlo integration in miniature: a hard quantity, an area, an integral, an expectation, becomes a simple average over random samples.
How Accurate? The 1/√N Law
Simulation is not magic, the estimate has its own random error. That error shrinks in proportion to 1/√N, where N is the number of trials. The honest, and slightly painful, consequence: to cut the error in half you need four times the samples; for one more decimal place, a hundred times.
On log-log axes, plotting error against N gives a straight line of slope −1/2, the fingerprint of 1/√N convergence. The takeaway is balance: simulation is trivially easy to write, but can be expensive to make precise. For a rough answer it is unbeatable; for high precision, an exact formula (when one exists) wins.
Because simulation uses randomness, two runs give slightly different answers. Fixing the random
seed (for example np.random.default_rng(36)) makes a run reproducible: you, and
anyone reading your work, get the exact same numbers every time. Reproducibility is a courtesy and a discipline,
not an optional extra.
When Intuition Fails: Monty Hall
Simulation's greatest gift is settling arguments. Three doors hide one car and two goats. You pick a door; the host, who knows where the car is, opens a different door to reveal a goat, then asks if you want to switch. Should you? Almost everyone says it cannot matter. Almost everyone is wrong.
Switching wins about 2/3 of the time, double the odds of staying. The reason: your first pick is right only 1/3 of the time, so 2/3 of the time the car is behind one of the other two doors, and the host helpfully removes the wrong one. The host's choice is not random, it leaks information. You can argue this in words forever; 200,000 simulated games end the debate in a second.
Monte Carlo in Machine Learning & AI
Monte Carlo is not a beginner's toy that real systems outgrow, it is everywhere in modern AI. Whenever a quantity is an expectation or an integral too hard to compute exactly, the answer is almost always: sample it.
| Monte Carlo idea | In ML / AI it appears as | Concrete example |
|---|---|---|
| Estimate an expectation | Reinforcement learning | average sampled rollouts to estimate expected reward |
| Sample a posterior | Bayesian inference (MCMC) | draw from P(parameters | data) when no formula exists |
| Resample your data | The bootstrap | a confidence interval for any statistic, no algebra |
| Random forward passes | Monte Carlo dropout | run a net many times to estimate its uncertainty |
| Search by random rollouts | Monte Carlo Tree Search | the planning engine behind AlphaGo |
The hardest quantities in machine learning are expectations over enormous or unknown distributions: an agent's expected return, a model's posterior over parameters, the uncertainty in a prediction. None has a closed form, so practitioners reach for the tool in this chapter, average a batch of samples. MCMC samples Bayesian posteriors; policy-gradient methods estimate reward gradients from sampled episodes; Monte Carlo dropout turns a single network into an uncertainty estimate; the bootstrap quantifies confidence with no formula; and Monte Carlo Tree Search let AlphaGo plan in a space with more positions than atoms in the universe. The simple dart-throwing loop you just wrote is, quite literally, a load-bearing pillar of modern AI.
Simulate it yourself
The companion notebook estimates a probability by counting (and matches the exact 0.5177), throws 100,000 random darts to approximate π with a scatter plot, measures the 1/√N error law on log-log axes, settles Monty Hall with 200,000 games (stay 1/3, switch 2/3), and bootstraps a 95% confidence interval from a single sample.
View opens the rendered notebook instantly (no setup). Open in Colab runs &
edits it live in your browser. To run locally, install numpy and matplotlib and launch
jupyter notebook.
🎓 Key Takeaways
- ✓Monte Carlo estimates a probability or expectation by simulating the experiment many times and averaging; it rests on the Law of Large Numbers.
- ✓The loop is: define the event, run one random trial, repeat N times, divide successes by N.
- ✓Estimating π with random darts shows the core trick: a hard area or integral becomes an average over samples.
- ✓Error shrinks like 1/√N: quadruple the samples to halve the error. Set a seed for reproducibility.
- ✓In ML/AI: reinforcement learning, MCMC, the bootstrap, MC dropout, and tree search (AlphaGo) are all Monte Carlo.
Practice Challenges
Five short challenges, beginner to intermediate. Try them in Python before checking the solutions, simulation is best learned by writing the loop yourself.
Estimate a probability
Simulate 200,000 sets of 3 fair coin flips and estimate P(at least one head). Compare to the exact value.
Estimate an expectation
Estimate E[max of two dice] by simulation. (Awkward by hand, easy to simulate.)
The birthday problem
In a room of 23 people, estimate P(at least two share a birthday). Prepare to be surprised.
Monte Carlo integration
Estimate the integral of x² from 0 to 1 by averaging f(U) for uniform U on [0,1].
Bootstrap a CI
Given a sample, estimate a 95% confidence interval for its median by resampling with replacement 10,000 times.
A fully-worked solutions notebook walks through all five challenges, each verified in code. Try them yourself first, then compare.
Quiz: Test Yourself
Eight quick questions on simulation and Monte Carlo. Answer them, hit Check Answers, and keep refining until you score 100%. Your progress is saved, so you can hop back to the chapter and return anytime.
You have built probability from outcomes and events all the way to Bayesian updating, expectation, and simulation. Next, the Probability Distributions part turns to the named distributions, the Bernoulli, binomial, Poisson, and the famous normal curve, that give these ideas their standard shapes.