Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 38 ยท Getting Started

Simulating Dice, Cards and Games

Turn game rules into code: the birthday problem, the Monty Hall puzzle and a gambler's ruin walk, each checked against exact answers.

Games are the friendliest training ground for Monte Carlo. The rules are clear, the randomness is easy to model, and many have exact answers you can compare with. In this lesson you will simulate three classic puzzles: the birthday problem, the Monty Hall problem and the gambler's ruin. Each one teaches a habit: write the rules as a function that returns one trial's outcome, then average.

A pattern for every game

flowchart LR
  A["Write rules as one_trial()"] --> B["Return 1 if event, else 0"]
  B --> C["Average over N trials"]
  C --> D["Add standard error"]
  D --> E["Compare with exact answer"]

Turning a game into a Monte Carlo estimate.

The birthday problem

How many people must be in a room before it is more likely than not that two share a birthday? Assume 365 equally likely birthdays and ignore leap years. Intuition often says about 180. The exact answer is surprising: with 23 people the chance already exceeds 50%.

The exact probability that all k birthdays are different is (365/365) ร— (364/365) ร— โ€ฆ ร— ((365-k+1)/365). The probability of at least one match is 1 minus that.

Python Birthday problem: simulation versus exact

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Both columns should agree to about two decimals. The trick is len(set(...)) < k: a set removes repeats, so a smaller size means a match.

The Monty Hall problem

You face three doors. A prize is behind one; the other two hide goats. You pick a door. The host, who knows where the prize is, opens a different door that hides a goat, then offers you the chance to switch to the remaining closed door. Should you switch?

Many people say it makes no difference. Let the simulation settle it.

Python Monty Hall: stay or switch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Switching wins about two thirds of the time. Why? Your first pick is right with probability 1/3. If you are wrong (probability 2/3), the host has to open the only other goat door, so the remaining door must hide the prize; switching wins in exactly those cases. The simulation makes the argument convincing because you can watch it happen rather than trust the words.

The result depends on the host's rule. If the host opened a random door and just happened to reveal a goat, the answer would change. Whenever a simulation disagrees with your intuition, compare the rules you coded with the rules in the problem.

Gambler's ruin

A gambler starts with 5 coins. Each round they win 1 coin or lose 1 coin with equal chance. They stop when they reach 10 coins or go broke (0 coins). What is the probability of reaching 10 before 0? For a fair game the exact answer is start / goal = 5/10 = 0.5. With a start of 2 it would be 0.2.

The game also asks a second question that is harder to answer by algebra: how many rounds does it take on average? Simulation gives both answers from one loop.

Python Gambler's ruin: win chance and game length

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For a fair game, the exact mean duration is start ร— (goal โˆ’ start): 16, 25 and 16 rounds for starts 2, 5 and 8. Your averages should land close to those numbers. Try changing p_win to 0.48: a tiny disadvantage makes ruin far more likely, which is why casinos win.

Shuffling and dealing

Card games use sampling without replacement. In NumPy, rng.permutation(52) shuffles a deck and rng.choice(52, 5, replace=False) deals a hand. Here is a quick check of the chance that a five-card hand holds at least one ace (exact value about 0.3412).

Python Aces in a five-card hand

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In the Monty Hall game where the host always opens a goat door, what is the win probability when you switch?

    You lose by switching only if your first pick was the prize (probability 1/3), so switching wins with probability 2/3.
  2. 2. In the birthday simulation, why does len(set(birthdays)) < k detect a match?

    If all k birthdays differ, the set has k elements. A smaller set means at least two people share a birthday.
  3. 3. A fair gambler's ruin game has start 3 and goal 12. What is the exact probability of reaching the goal?

    For a fair game the winning probability is start / goal = 3/12 = 0.25.
  4. 4. What should you do first when a simulation result contradicts your intuition?

    Surprising results often reveal either a bug or a flawed intuition; comparing the coded rules with the real rules decides which.

Summary

  • Write the rules of a game as a function that plays one trial and returns the outcome.
  • The birthday problem: with 23 people the chance of a shared birthday already exceeds 50%.
  • Monty Hall: switching wins 2/3 of the time when the host knowingly reveals a goat.
  • Gambler's ruin simulations produce both the win probability and the expected game length.
  • Compare with an exact answer whenever one exists; it is the best test of your code.

Next: Pseudorandom Numbers and Seeds