Birthday and matching problems · Mathematics Class 11–12 | StemPy Lab
Course 23.3·Lesson 2 of 3
Birthday and matching problems
Two classic problems show how counting beats intuition. In a room of twenty-three people, two share a birthday more often than not. When letters are put into envelopes at random, the chance that no letter is in the right envelope settles near one divided by e, however many letters there are. This lesson counts both, with the complement and with inclusion and exclusion.
about 30 min2 simulations1 practice question
Lesson 2 of 3Show all lessonsHide
Try the code and the simulations beside the lesson.
What you will be able to do
find the probability that n people all have different birthdays, and that at least two share one;
count the ways to put n letters in n envelopes with no letter in its own envelope, called derangements;
find the chance of exactly r matches.
The room of twenty-three
How many people must be in a room for it to be more likely than not that two of them share a birthday? Many people guess a number near 180, half of 365. The true answer is 23. We need a model first. Assume that there are 365 days (ignore a leap day), that each person's birthday is equally likely to be any day, and that people are independent.
The complement is easier. The probability that all n people have different birthdays is
P=365365×365364×⋯×365365−n+1=365n365Pn.
The second person must avoid one day, the third must avoid two, and so on. So
P(at least two share)=1−365n365Pn.
For n=10 it is about 0.117, for n=23 about 0.507, for n=30 about 0.706 and for n=50 about 0.970. The chance grows quickly because the number of pairs of people grows like 2n2. Twenty-three people make 253 pairs, and each pair has a chance 3651 of sharing a birthday.
A graph of the chance that at least two people in a group share a birthday, against the number of people from 1 to 60. The curve starts near 0, rises through the dashed line at one half at 23 people, where a point is marked at 0.507, and is close to 1 at 60 people.
The same idea works for any number of boxes. If n objects each land in one of N equally likely boxes, the chance that all land in different boxes is NnNPn. For five people and 12 months of birth, it is 12512×11×10×9×8.
A different question. The chance that someone else shares your birthday in a room of n people is 1−(365364)n−1. You need 253 other people for an even chance. Do not mix the two questions.
Letters in envelopes
A secretary writes n letters and n addressed envelopes, and then puts the letters in the envelopes at random, one in each. A match is a letter in its own envelope. What is the chance that there is no match?
An outcome is a permutation of the n letters, so there are n! outcomes. We count the permutations with no fixed point. They are called derangements, and their number is Dn.
By inclusion and exclusion, let Ai be "letter i is in the right envelope". Then P(Ai)=n!(n−1)!=n1, and for any k letters, P(all of them right)=n!(n−k)!. There are nCk ways to choose the k letters, so the sum for k letters is nCk×n!(n−k)!=k!1. So
P(at least one match)=1−2!1+3!1−⋯
P(no match)=n!Dn=k=0∑nk!(−1)k.
The values are D1=0, D2=1, D3=2, D4=9, D5=44, D6=265. A shortcut is Dn=(n−1)(Dn−1+Dn−2), so D6=5×(44+9)=265.
The sum ∑k!(−1)k is the start of the series for e−1. So for large n, the chance of no match is close to e1≈0.3679. Already for n=6 it is 720265≈0.3681.
A bar chart of the chance that no letter goes into its own envelope, for n from 1 to 10 letters. The bars are 0 for one letter, 0.5 for two, 0.333 for three, 0.375 for four, and then settle close to a dashed line at 1 over e, which is 0.368. After about 6 letters the bars cannot be told from the line.
Exactly r matches
To get exactly r matches, choose which r letters are in the right envelope, nCr ways. The other n−r letters must all be wrong, which is Dn−r ways. So
P(exactly r matches)=n!nCr×Dn−r.
For n=4: no match is 249, exactly one is 244×2=248, exactly two is 246×1, exactly three is 244×0=0, and four is 241. These add up to 2424. Exactly three matches is impossible, because if three letters are right, the fourth must be right too.
A worked example
Five students hand in their papers. The teacher returns the papers at random, one to each student. Find the probability that no student gets the right paper, and that exactly one does.
No match.D5=44 out of 5!=120, so the probability is 12044=3011.
Exactly one. Choose the student, 5 ways. The other four must all be wrong, D4=9 ways. So 1205×9=12045=83.
Check by recurrence.D5=4×(D4+D3)=4×(9+2)=44. It agrees.
Practise with new numbers
Try it in Python
The program finds the chance of a shared birthday in exact fractions for a few group sizes. It then lists all the permutations of n letters, counts those with no letter in place, and compares the count with the formula and with e1.
Common mistakes
Taking half of 365. A shared birthday needs a pair, and the number of pairs grows quickly.
Mixing "some two share" with "someone shares mine". They are different events.
Dropping the factor nCr. Exactly r matches also needs the choice of which r letters match.
Forgetting that exactly n−1 matches is impossible. If n−1 letters are right, so is the last.
Remember these
All different birthdays: 365n365Pn. At least two share: one minus that.