The idea in one sentence
A Monte Carlo method estimates an answer by doing something random over and over and averaging what happens. Instead of solving a probability problem with algebra, you act it out many times and count.
The name comes from the casino in Monaco, but nothing about the method involves gambling. It is just repeated random sampling.
The problem
Deal five cards from a standard 52-card deck. What is the chance that at least one is an ace?
This question has an exact answer, which makes it a useful practice case because we can check the Monte Carlo result against it. The usual trick is to find the chance of no aces and subtract from 1.
The chance that all five cards avoid the four aces is:
(48/52) × (47/51) × (46/50) × (45/49) × (44/48)
The 48s cancel, leaving (47 × 46 × 45 × 44) / (52 × 51 × 50 × 49). The top is 4,280,760 and the bottom is 6,497,400, giving about 0.6588. So the chance of at least one ace is about 1 − 0.6588 = 0.3412, or roughly 34.1 percent. The same logic underlies the idea of at least one success in any repeated chance.
Step 1: Describe one trial
A trial is one deal of five cards. Represent the deck as 52 items, four of them aces. Shuffle, take five, and check whether any is an ace.
Step 2: Write it in code
import random
deck = ["ace"] * 4 + ["other"] * 48
def hand_has_ace():
hand = random.sample(deck, 5)
return "ace" in hand
trials = 100_000
hits = sum(hand_has_ace() for _ in range(trials))
print(hits / trials)
random.sample picks five distinct items without replacement, which matches dealing from a deck. The printed result should be close to 0.34.
Step 3: Understand why it works
Each trial gives a yes or no. The share of “yes” results approaches the true probability as trials accumulate, a behavior known as the law of large numbers. After 10 trials the estimate could easily be 0.2 or 0.5. After 100,000 it should differ from 0.341 by well under a percentage point.
Step 4: Measure the error
The estimate has a standard error of √(p × (1 − p) / n). Using p near 0.341:
- With 1,000 trials: √(0.2247 / 1000) is about 0.015, or 1.5 percentage points.
- With 10,000 trials: about 0.0047, or 0.5 percentage points.
- With 100,000 trials: about 0.0015, or 0.15 percentage points.
About 95 percent of the time, a run should land within roughly two standard errors of the truth. Ten times the work buys only a bit more than three times the precision, which is the main cost of the method.
Step 5: Use it where algebra is harder
The ace problem was solvable by hand, so the simulation was a rehearsal. The value shows up when the rules are awkward. Suppose a game lets you redraw any non-ace once, or ends the hand when two cards of the same suit appear. Writing an exact formula becomes laborious, but the code only needs a few more lines. Questions about opening hands with mulligans are a typical case, where a hypergeometric distribution handles the basic draw but extra rules make an exact answer messy.
Common mistakes
- Sampling with replacement by accident. Drawing cards one at a time and putting them back changes the probability.
- Forgetting to shuffle or re-create the deck between trials, which makes consecutive hands depend on each other.
- Trusting a single small run. Run the program a few times. If the answers wander widely, increase the trials.
- Treating the estimate as exact. Report it with its uncertainty, for example “about 34 percent, give or take half a point.”
- Skipping a sanity test. Before modeling a complicated rule, reproduce an answer you know, as done here.
Reproducing your results
Because the output changes on each run, it helps to fix the generator’s starting point when you want to compare versions of your code or share a result. That technique is covered under random seeds. Without it, small differences between two runs may be noise rather than an effect of your change.
What Monte Carlo cannot do
It cannot make a poorly specified model correct. If the simulated deck is wrong or a rule is missed, the answer will be confidently wrong. It also struggles with very rare events, where nearly all trials show nothing and the estimate is unstable. And it says nothing about how an opponent will act. For those reasons, it works best as a partner to exact reasoning rather than a replacement. When both are available, agreement between them is the best evidence that the work is right.