How to Animate a Birthday Paradox Simulation

Start with one shared birthday, then animate the running share of groups with at least one match. For 23 people with independent birthdays equally likely across 365 days, ignoring leap day, the theoretical chance of a match is about 50.73%.

Run the Python exercise

Count groups with a match, not matching pairs

Reveal the day labels 12, 47, and 12 in order. Mark both 12s when the last card appears and show “Match: yes.” This is one group with a match. A group with labels 12, 47, 12, and 47 also counts once. These small groups illustrate the rule; they are not part of the 23-person simulation.

Run 1,000 groups with a fixed seed

Save this as birthdays.py and run python3 birthdays.py > birthdays.csv. Each row records the group number, whether that group matched (1 or 0), the total matches so far, and the running percentage. The seed lets you repeat the run without choosing an ending for the chart.

from random import Random


def has_match(days):
    return len(set(days)) < len(days)


assert not has_match([12, 47, 90])
assert has_match([12, 47, 12])
assert int(has_match([12, 47, 12, 47])) == 1

rng = Random(42)
matches = 0
print("group,matched,groups_with_match,running_percent")
for group in range(1, 1001):
    days = [rng.randint(1, 365) for _ in range(23)]
    matched = int(has_match(days))
    matches += matched
    print(f"{group},{matched},{matches},{100 * matches / group:.2f}")

Animate the CSV in three shots

  1. Reveal the three example cards and mark the duplicate. Add the caption “A group counts once if any birthdays match.”
  2. Switch to the simulated groups. Keep fixed axes labeled “Groups tested” (0 to 1,000) and “Groups with a match (%)” (0 to 100). Reveal the line in row order, starting at group 1. Update the displayed count of matching groups and the number tested from the same row as the line tip.
  3. Draw a dashed line at 50.73% and label it “Theory for 23 people.” Hold the last simulated point and its count. Leave the gap visible if the point ends above or below the reference.

The reference comes from calculating the chance that all 23 birthdays are different: multiply 365/365 by 364/365, continuing through 343/365. Subtract that product from 1 and convert to a percentage. A finite run can finish on either side of the theoretical value.

Check the animation without sound

Pause after revealing the duplicate and check that the group counts once. On the graph, pause at groups 100 and 1,000 and compare the on-screen counts and percentage with those CSV rows. If the graph appears to start at 0%, remove the point at group 0: the share is undefined before any groups are tested.

Open the Birthday Probability Lab Skill reference if you want a separate Loretry example.