Introduction
Some probabilities are hard to count: the chance of winning a game of solitaire, of a queue overflowing, of a portfolio losing money. A computer can play the game a million times instead. Simulation, or the Monte Carlo method after the casino, estimates a probability by repeating a random experiment many times and counting how often the event happens.
How it works
Run the experiment n times and count the successes. The relative frequency, successes ÷ n, estimates the probability. The law of large numbers says this estimate settles near the true value as n grows. It never stops wobbling completely: the typical error shrinks like 1 ÷ √n, so a hundred times more trials buys only ten times more accuracy. With 300 trials, an estimate of a probability near 1/6 is usually within about 0.04 of it.
Python's random module provides the randomness: random.randint(1, 6) rolls a die and random.random() gives a number between 0 and 1. These numbers are pseudo-random: a formula produces them from a starting value, the seed. random.seed(7) makes the "random" draws the same on every run, so a result can be reproduced, checked and debugged. Real simulations are seeded too, for exactly that reason.
A simulation is also a check on reasoning. The famous Monty Hall puzzle (a prize behind one of three doors; after your pick the host opens an empty door and offers a switch) fools most people, who think switching does not matter. Simulating a few thousand games shows that switching wins about two thirds of the time, which the exact argument then explains: switching wins whenever your first pick was wrong.
Example
import random
random.seed(7)
trials = 300
sevens = 0
for _ in range(trials):
if random.randint(1, 6) + random.randint(1, 6) == 7:
sevens += 1
print("sevens:", sevens, "of", trials)
print("estimate:", round(sevens / trials, 3), " exact:", round(1 / 6, 3))Line by line
random.seed(7)fixes the sequence, so the run is the same every time.- Each trial rolls two dice and counts a success when they add up to 7.
- The estimate is the share of successes; the exact value 1/6 is printed beside it for comparison.
Expected result
sevens: 43 of 300
estimate: 0.143 exact: 0.167
The estimate is near 1/6 but not equal to it: 300 trials are enough to see the pattern, not to pin it down.
In the real world
Banks simulate thousands of possible futures to measure risk, engineers simulate traffic and network load, physicists simulate particles, and weather forecasts run the same model many times with slightly different starting values. Games and sports analysts simulate whole seasons to estimate who will win. Whenever an exact answer is out of reach, a well-designed simulation gives an estimate with a known accuracy.
Common mistakes
- Trusting a small simulation: with 20 trials, an estimate can easily be off by 0.15.
- Forgetting to seed, so a result cannot be reproduced.
- Simulating the wrong experiment, for example rolling one die and doubling it instead of rolling two dice.
Best practices
- Check a simulation on a case you can work out exactly before trusting it on one you cannot.
- Increase the number of trials until the estimate stops changing in the digits you care about.
- Report an estimate with its likely error, not as an exact number.
Summary
Simulation estimates a probability by the relative frequency of an event over many random trials. The law of large numbers makes the estimate settle near the truth, with an error that shrinks like 1 ÷ √n. Seeding the generator makes the run repeatable, and checking the simulation on a known case makes it trustworthy.
Quick check
Key takeaways
- When counting or formulas are hard, simulate: the share of successes estimates the probability.
- More trials give better estimates, but the error shrinks only with the square root of the number of trials.
- Seed the generator so that a simulation can be repeated and checked.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.