Birthday problem



In probability theory, the birthday problem asks for the probability that, in a set of $n$ randomly chosen people, at least two will share a birthday. The birthday paradox refers to the counterintuitive fact that only 23 people are needed for that probability to exceed 50%.

The birthday paradox is a veridical paradox: it seems wrong at first glance but is, in fact, true. While it may seem surprising that only 23 individuals are required to reach a 50% probability of a shared birthday, this result is made more intuitive by considering that the birthday comparisons will be made between every possible pair of individuals. With 23 individuals, there are $23 × 22⁄2$ = 253 pairs to consider, far more than half the number of days in a year.

Real-world applications for the birthday problem include a cryptographic attack called the birthday attack, which uses this probabilistic model to reduce the complexity of finding a collision for a hash function, as well as calculating the approximate risk of a hash collision existing within the hashes of a given size of population.

The problem is generally attributed to Harold Davenport in about 1927, though he did not publish it at the time. Davenport did not claim to be its discoverer "because he could not believe that it had not been stated earlier". The first publication of a version of the birthday problem was by Richard von Mises in 1939.

Calculating the probability
From a permutations perspective, let the event $A$ be the probability of finding a group of 23 people without any repeated birthdays. Where the event $B$ is the probability of finding a group of 23 people with at least two people sharing same birthday, $P(B) = 1 − P(A)$. $P(A)$ is the ratio of the total number of birthdays, $$V_{nr}$$, without repetitions and order matters (e.g. for a group of 2 people, mm/dd birthday format, one possible outcome is $$\left \{ \left \{01/02,05/20\right \},\left \{05/20,01/02\right \},\left \{10/02,08/04\right\},...\right \}$$) divided by the total number of birthdays with repetition and order matters, $$V_{t}$$, as it is the total space of outcomes from the experiment (e.g. 2 people, one possible outcome is $$\left \{ \left \{01/02,01/02\right \},\left \{10/02,08/04\right \},...\right \}$$). Therefore $$V_{nr}$$ and $$V_{t}$$ are permutations.


 * $$\begin{align} V_{nr} &= \frac{n!}{(n-k)!} = \frac{365!}{(365-23)!} \\[8pt] V_t &= n^k = 365^{23} \\[8pt] P(A) &= \frac{V_{nr}}{V_t} \approx 0.492703 \\[8pt] P(B) &= 1 - P(A) \approx 1 - 0.492703 \approx 0.507297 \quad (50.7297\%)\end{align}$$

Another way the birthday problem can be solved is by asking for an approximate probability that in a group of $n$ people at least two have the same birthday. For simplicity, leap years, twins, selection bias, and seasonal and weekly variations in birth rates are generally disregarded, and instead it is assumed that there are 365 possible birthdays, and that each person's birthday is equally likely to be any of these days, independent of the other people in the group.

For independent birthdays, a uniform distribution of birthdays minimizes the probability of two people in a group having the same birthday. Any unevenness increases the likelihood of two people sharing a birthday. However real-world birthdays are not sufficiently uneven to make much change: the real-world group size necessary to have a greater than 50% chance of a shared birthday is 23, as in the theoretical uniform distribution.

The goal is to compute $P(B)$, the probability that at least two people in the room have the same birthday. However, it is simpler to calculate $P(A′)$, the probability that no two people in the room have the same birthday. Then, because $B$ and $A′$ are the only two possibilities and are also mutually exclusive, $P(B) = 1 − P(A′).$

Here is the calculation of $P(B)$ for 23 people. Let the 23 people be numbered 1 to 23. The event that all 23 people have different birthdays is the same as the event that person 2 does not have the same birthday as person 1, and that person 3 does not have the same birthday as either person 1 or person 2, and so on, and finally that person 23 does not have the same birthday as any of persons 1 through 22. Let these events be called Event 2, Event 3, and so on. Event 1 is the event of person 1 having a birthday, which occurs with probability 1. This conjunction of events may be computed using conditional probability: the probability of Event 2 is $364⁄365$, as person 2 may have any birthday other than the birthday of person 1. Similarly, the probability of Event 3 given that Event 2 occurred is $363⁄365$, as person 3 may have any of the birthdays not already taken by persons 1 and 2. This continues until finally the probability of Event 23 given that all preceding events occurred is $343⁄365$. Finally, the principle of conditional probability implies that $P(A′)$ is equal to the product of these individual probabilities:

The terms of equation ($$) can be collected to arrive at:

Evaluating equation ($$) gives $P(A′) ≈ 0.492703$

Therefore, $P(B) ≈ 1 − 0.492703 = 0.507297$ (50.7297%).

This process can be generalized to a group of $$ people, where $p(n)$ is the probability of at least two of the $$ people sharing a birthday. It is easier to first calculate the probability $\overline{p}(n)$ that all $n$ birthdays are different. According to the pigeonhole principle, $\overline{p}(n)$ is zero when $n > 365$. When $n ≤ 365$:


 * $$ \begin{align} \bar p(n) &= 1 \times \left(1-\frac{1}{365}\right) \times \left(1-\frac{2}{365}\right) \times \cdots \times \left(1-\frac{n-1}{365}\right) \\[6pt] &= \frac{ 365 \times 364 \times \cdots \times (365-n+1) }{ 365^n } \\[6pt] &= \frac{ 365! }{ 365^n (365-n)!} = \frac{n!\cdot\binom{365}{n}}{365^n} = \frac{_{365}P_n}{365^n}\end{align} $$

where $!$ is the factorial operator, $(365 n)$ is the binomial coefficient and $_{k}P_{r}$ denotes permutation.

The equation expresses the fact that the first person has no one to share a birthday, the second person cannot have the same birthday as the first $(364⁄365)$, the third cannot have the same birthday as either of the first two $(363⁄365)$, and in general the $n$th birthday cannot be the same as any of the $n − 1$ preceding birthdays.

The event of at least two of the $n$ persons having the same birthday is complementary to all $n$ birthdays being different. Therefore, its probability $p(n)$ is


 * $$ p(n) = 1 - \bar p(n). $$

The following table shows the probability for some other values of $n$ (for this table, the existence of leap years is ignored, and each birthday is assumed to be equally likely):




 * {| class="wikitable"

!$n$!!$p(n)$
 * align=right|1 || 0.0%
 * align=right|5 || 2.7%
 * align=right|10 || 11.7%
 * align=right|20 || 41.1%
 * align=right|23 || 50.7%
 * align=right|30 || 70.6%
 * align=right|40 || 89.1%
 * align=right|50 || 97.0%
 * align=right|60 || 99.4%
 * align=right|70 || 99.9%
 * align=right|75 || 99.97%
 * align=right|100 || $n$%
 * align=right|200 || $n$%
 * align=right|300 || (100 − $n$)%
 * align=right|350 || (100 − $100$)%
 * align=right|365 || (100 − $100$)%
 * align=right|≥ 366 || 100%
 * }
 * align=right|70 || 99.9%
 * align=right|75 || 99.97%
 * align=right|100 || $6$%
 * align=right|200 || $3$%
 * align=right|300 || (100 − $1.45$)%
 * align=right|350 || (100 − $a$)%
 * align=right|365 || (100 − $d$)%
 * align=right|≥ 366 || 100%
 * }
 * align=right|300 || (100 − $n$)%
 * align=right|350 || (100 − $n$)%
 * align=right|365 || (100 − $d$)%
 * align=right|≥ 366 || 100%
 * }
 * align=right|≥ 366 || 100%
 * }
 * }

Approximations
The Taylor series expansion of the exponential function (the constant $e ≈ 2.718$)


 * $$ e^x = 1 + x + \frac{x^2}{2!}+\cdots $$

provides a first-order approximation for $e^{x}$ for $$|x| \ll 1$$:


 * $$ e^x \approx 1 + x.$$

To apply this approximation to the first expression derived for $\overline{p}(n)$, set $x = −a⁄365$. Thus,
 * $$ e^{-a/365} \approx 1 - \frac{a}{365}. $$

Then, replace $364⁄365$ with non-negative integers for each term in the formula of $\overline{p}(n)$ until $a = n − 1$, for example, when $a = 1$,
 * $$ e^{-1/365} \approx 1 - \frac{1}{365}. $$

The first expression derived for $\overline{p}(n)$ can be approximated as



\begin{align} \bar p(n) & \approx 1 \cdot e^{-1/365} \cdot e^{-2/365} \cdots e^{-(n-1)/365} \\[6pt] & = e^{-\big(1+2+ \,\cdots\, +(n-1)\big)/365} \\[6pt] & = e^{-\frac{n(n-1)/2}{365}} = e^{-\frac{n(n-1)}{730}}. \end{align} $$

Therefore,


 * $$ p(n) = 1-\bar p(n) \approx 1 - e^{-\frac{n(n-1)}{730}}.$$

An even coarser approximation is given by


 * $$p(n)\approx 1-e^{-\frac{n^2}{730}},$$

which, as the graph illustrates, is still fairly accurate.

According to the approximation, the same approach can be applied to any number of "people" and "days". If rather than 365 days there are $364⁄365$, if there are $1⁄2$ persons, and if $n ≪ d$, then using the same approach as above we achieve the result that if $p(n, d)$ is the probability that at least two out of $d$ people share the same birthday from a set of $1⁄2$ available days, then:


 * $$\begin{align}

p(n, d) & \approx 1-e^{-\frac{n(n-1)}{2d}} \\[6pt] & \approx 1-e^{-\frac{n^2}{2d}}. \end{align}$$

Simple exponentiation
The probability of any two people not having the same birthday is $1⁄2$. In a room containing n people, there are $(n 2) = n(n − 1)⁄2$ pairs of people, i.e. $(n 2)$ events. The probability of no two people sharing the same birthday can be approximated by assuming that these events are independent and hence by multiplying their probability together. Being independent would be equivalent to picking with replacement, any pair of people in the world, not just in a room. In short $1⁄2$ can be multiplied by itself $(n 2)$ times, which gives us


 * $$\bar p(n) \approx \left(\frac{364}{365}\right)^\binom{n}{2}.$$

Since this is the probability of no one having the same birthday, then the probability of someone sharing a birthday is


 * $$p(n) \approx 1 - \left(\frac{364}{365}\right)^\binom{n}{2}.$$

And for the group of 23 people, the probability of sharing is


 * $$p(23) \approx 1 - \left(\frac{364}{365}\right)^\binom{23}{2} = 1 - \left(\frac{364}{365}\right)^{253} \approx 0.500477 .$$

Poisson approximation
Applying the Poisson approximation for the binomial on the group of 23 people,


 * $$\operatorname{Poi}\left(\frac{\binom{23}{2}}{365}\right) =\operatorname{Poi}\left(\frac{253}{365}\right) \approx \operatorname{Poi}(0.6932)$$

so


 * $$\Pr(X>0)=1-\Pr(X=0) \approx 1-e^{-0.6932} \approx 1-0.499998=0.500002.$$

The result is over 50% as previous descriptions. This approximation is the same as the one above based on the Taylor expansion that uses $e^{x} ≈ 1 + x$.

Square approximation
A good rule of thumb which can be used for mental calculation is the relation


 * $$p(n,d) \approx \frac{n^2}{2d}$$

which can also be written as


 * $$n \approx \sqrt { 2d \times p(n)}$$

which works well for probabilities less than or equal to $b$. In these equations, $p$ is the number of days in a year.

For instance, to estimate the number of people required for a $p$ chance of a shared birthday, we get


 * $$n \approx \sqrt{ 2 \times 365 \times \tfrac12} = \sqrt{365} \approx 19$$

Which is not too far from the correct answer of 23.

Approximation of number of people
This can also be approximated using the following formula for the number of people necessary to have at least a $$ chance of matching:
 * $$n \geq \tfrac{1}{2} + \sqrt{\tfrac{1}{4} + 2 \times \ln(2) \times 365} = 22.999943.$$

This is a result of the good approximation that an event with $1⁄k$ probability will have a $p$ chance of occurring at least once if it is repeated $k ln 2$ times.

Probability table

 * {| class="wikitable" style="white-space:nowrap;"

! rowspan="2" | length of hex string ! rowspan="2" | no. of bits ($$) ! rowspan="2" | hash space size ($2^{b}$) ! colspan="10" | Number of hashed elements such that probability of at least one hash collision ≥ $p$ ! $$ = $p$ ! $$ = $p$ ! $$ = $p$ ! $p$ = $p$ ! $p$ = $p$ ! $4.3$ = 0.001 ! $2.9$ = 0.01 ! $9.3$ = 0.25 ! $5$ = 0.50 ! $7.7$ = 0.75 The lighter fields in this table show the number of hashes needed to achieve the given probability of collision (column) given a hash space of a certain size in bits (row). Using the birthday analogy: the "hash space size" resembles the "available days", the "probability of collision" resembles the "probability of shared birthday", and the "required number of hashed elements" resembles the "required number of people in a group". One could also use this chart to determine the minimum hash size required (given upper bounds on the hashes and probability of error), or the probability of collision (for fixed number of hashes and probability of error).
 * - align="center"
 * bgcolor="#F2F2F2" | 8
 * bgcolor="#F2F2F2" | 32
 * bgcolor="#F2F2F2" | $1.1$
 * 2
 * 2
 * 2
 * 2.9
 * 93
 * - align="center"
 * bgcolor="#F2F2F2" | (10)
 * bgcolor="#F2F2F2" | (40)
 * bgcolor="#F2F2F2" | ($1.1$)
 * 2
 * 2
 * 2
 * 47
 * - align="center"
 * bgcolor="#F2F2F2" | (12)
 * bgcolor="#F2F2F2" | (48)
 * bgcolor="#F2F2F2" | ($1.5$)
 * 2
 * 2
 * 24
 * - align="center"
 * bgcolor="#F2F2F2" | 16
 * bgcolor="#F2F2F2" | 64
 * bgcolor="#F2F2F2" | $4.7$
 * 6.1
 * - align="center"
 * bgcolor="#F2F2F2" | (24)
 * bgcolor="#F2F2F2" | (96)
 * bgcolor="#F2F2F2" | ($1.5$)
 * - align="center"
 * bgcolor="#F2F2F2" | 32
 * bgcolor="#F2F2F2" | 128
 * bgcolor="#F2F2F2" | $8$
 * - align="center"
 * bgcolor="#F2F2F2" | (48)
 * bgcolor="#F2F2F2" | (192)
 * bgcolor="#F2F2F2" | ($1.2$)
 * - align="center"
 * bgcolor="#F2F2F2" | 64
 * bgcolor="#F2F2F2" | 256
 * bgcolor="#F2F2F2" | $1.7$
 * - align="center"
 * bgcolor="#F2F2F2" | (96)
 * bgcolor="#F2F2F2" | (384)
 * bgcolor="#F2F2F2" | ($2.8$)
 * - align="center"
 * bgcolor="#F2F2F2" | 128
 * bgcolor="#F2F2F2" | 512
 * bgcolor="#F2F2F2" | $7.5$
 * }
 * - align="center"
 * bgcolor="#F2F2F2" | (24)
 * bgcolor="#F2F2F2" | (96)
 * bgcolor="#F2F2F2" | ($2.4$)
 * - align="center"
 * bgcolor="#F2F2F2" | 32
 * bgcolor="#F2F2F2" | 128
 * bgcolor="#F2F2F2" | $7.5$
 * - align="center"
 * bgcolor="#F2F2F2" | (48)
 * bgcolor="#F2F2F2" | (192)
 * bgcolor="#F2F2F2" | ($2.4$)
 * - align="center"
 * bgcolor="#F2F2F2" | 64
 * bgcolor="#F2F2F2" | 256
 * bgcolor="#F2F2F2" | $1.3$
 * - align="center"
 * bgcolor="#F2F2F2" | (96)
 * bgcolor="#F2F2F2" | (384)
 * bgcolor="#F2F2F2" | ($2$)
 * - align="center"
 * bgcolor="#F2F2F2" | 128
 * bgcolor="#F2F2F2" | 512
 * bgcolor="#F2F2F2" | $2.8$
 * }
 * - align="center"
 * bgcolor="#F2F2F2" | (48)
 * bgcolor="#F2F2F2" | (192)
 * bgcolor="#F2F2F2" | ($1.8$)
 * - align="center"
 * bgcolor="#F2F2F2" | 64
 * bgcolor="#F2F2F2" | 256
 * bgcolor="#F2F2F2" | $1.9$
 * - align="center"
 * bgcolor="#F2F2F2" | (96)
 * bgcolor="#F2F2F2" | (384)
 * bgcolor="#F2F2F2" | ($6.1$)
 * - align="center"
 * bgcolor="#F2F2F2" | 128
 * bgcolor="#F2F2F2" | 512
 * bgcolor="#F2F2F2" | $1.9$
 * }
 * - align="center"
 * bgcolor="#F2F2F2" | 64
 * bgcolor="#F2F2F2" | 256
 * bgcolor="#F2F2F2" | $6.1$
 * - align="center"
 * bgcolor="#F2F2F2" | (96)
 * bgcolor="#F2F2F2" | (384)
 * bgcolor="#F2F2F2" | ($1.9$)
 * - align="center"
 * bgcolor="#F2F2F2" | 128
 * bgcolor="#F2F2F2" | 512
 * bgcolor="#F2F2F2" | $6.1$
 * }
 * - align="center"
 * bgcolor="#F2F2F2" | (96)
 * bgcolor="#F2F2F2" | (384)
 * bgcolor="#F2F2F2" | ($3.3$)
 * - align="center"
 * bgcolor="#F2F2F2" | 128
 * bgcolor="#F2F2F2" | 512
 * bgcolor="#F2F2F2" | $5.1$
 * }
 * - align="center"
 * bgcolor="#F2F2F2" | 128
 * bgcolor="#F2F2F2" | 512
 * bgcolor="#F2F2F2" | $7.2$
 * }
 * - align="center"
 * bgcolor="#F2F2F2" | 128
 * bgcolor="#F2F2F2" | 512
 * bgcolor="#F2F2F2" | $7.9$
 * }
 * bgcolor="#F2F2F2" | 512
 * bgcolor="#F2F2F2" | $4$
 * }
 * }
 * }
 * }
 * }
 * }
 * }
 * }
 * }
 * }
 * }

For comparison, $1.3$ to $4$ is the uncorrectable bit error rate of a typical hard disk. In theory, 128-bit hash functions, such as MD5, should stay within that range until about $1.3$ documents, even if its possible outputs are many more.

An upper bound on the probability and a lower bound on the number of people
The argument below is adapted from an argument of Paul Halmos.

As stated above, the probability that no two birthdays coincide is


 * $$1-p(n) = \bar p(n) = \prod_{k=1}^{n-1}\left(1-\frac{k}{365}\right) .$$

As in earlier paragraphs, interest lies in the smallest $4$ such that $p(n) > 1⁄2$; or equivalently, the smallest $1.3$ such that $\overline{p}(n) < 1⁄2$.

Using the inequality $1 − x < e^{−x}$ in the above expression we replace $1 − k⁄365$ with $e^$. This yields


 * $$\bar p(n) = \prod_{k=1}^{n-1}\left(1-\frac{k}{365}\right) < \prod_{k=1}^{n-1}\left(e^{-\frac{k}{365}}\right) = e^{-\frac{n(n-1)}{730}} .$$

Therefore, the expression above is not only an approximation, but also an upper bound of $\overline{p}(n)$. The inequality


 * $$ e^{-\frac{n(n-1)}{730}} < \frac{1}{2}$$

implies $\overline{p}(n) < 1⁄2$. Solving for $4$ gives


 * $$n^2-n > 730 \ln 2 .$$

Now, $730 ln 2$ is approximately 505.997, which is barely below 506, the value of $n^{2} − n$ attained when $n = 23$. Therefore, 23 people suffice. Incidentally, solving $n^{2} − n = 730 ln 2$ for n gives the approximate formula of Frank H. Mathis cited above.

This derivation only shows that at most 23 people are needed to ensure the chances of a birthday match are at least even; it leaves open the possibility that $2.1$ is 22 or less could also work.

Arbitrary number of days
Given a year with $3.3$ days, the generalized birthday problem asks for the minimal number $n(d)$ such that, in a set of $4.7$ randomly chosen people, the probability of a birthday coincidence is at least 50%. In other words, $n(d)$ is the minimal integer $3.4$ such that


 * $$1-\left(1-\frac{1}{d}\right)\left(1-\frac{2}{d}\right)\cdots\left(1-\frac{n-1}{d}\right)\geq \frac{1}{2}.$$

The classical birthday problem thus corresponds to determining $n(365)$. The first 99 values of $n(d)$ are given here :


 * {| class="wikitable" style="text-align:center;"

! scope="row" | $2.6$ ! scope="row" | $n(d)$
 * 1–2 || 3–5 || 6–9 || 10–16 || 17–23 || 24–32 || 33–42 || 43–54 || 55–68 || 69–82 || 83–99
 * 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || 10 || 11 || 12
 * }

A similar calculation shows that $n(d)$ = 23 when $8.2$ is in the range 341–372.

A number of bounds and formulas for $n(d)$ have been published. For any $d ≥ 1$, the number $n(d)$ satisfies


 * $$\frac{3-2\ln2}{6}<n(d)-\sqrt{2d\ln2}\leq 9-\sqrt{86\ln2}.$$

These bounds are optimal in the sense that the sequence $n(d) − √2d ln 2$ gets arbitrarily close to
 * $$\frac{3-2\ln2}{6} \approx 0.27,$$

while it has
 * $$9-\sqrt{86\ln2}\approx 1.28$$

as its maximum, taken for $d = 43$.

The bounds are sufficiently tight to give the exact value of $n(d)$ in most of the cases. For example, for $d =$ 365 these bounds imply that $22.7633 < n(365) < 23.7736$ and 23 is the only integer in that range. In general, it follows from these bounds that $n(d)$ always equals either
 * $$\left\lceil\sqrt{2d\ln2}\,\right\rceil \quad\text{or}\quad \left\lceil\sqrt{2d\ln2}\,\right\rceil+1$$

where $⌈ · ⌉$ denotes the ceiling function. The formula


 * $$n(d) = \left\lceil\sqrt{2d\ln2}\,\right\rceil$$

holds for 73% of all integers $2.6$. The formula


 * $$n(d) = \left\lceil\sqrt{2d\ln2}+\frac{3-2\ln2}{6}\right\rceil$$

holds for almost all $8.2$, i.e., for a set of integers $2.6$ with asymptotic density 1.

The formula


 * $$n(d)=\left\lceil \sqrt{2d\ln2}+\frac{3-2\ln2}{6}+\frac{9-4(\ln2)^2}{72\sqrt{2d\ln2}}\right\rceil$$

holds for all $d ≤$, but it is conjectured that there are infinitely many counterexamples to this formula.

The formula


 * $$n(d)=\left\lceil \sqrt{2d\ln2}+\frac{3-2\ln2}{6}+\frac{9-4(\ln2)^2}{72\sqrt{2d\ln2}}-\frac{2(\ln2)^2}{135d}\right\rceil$$

holds for all $d ≤$, and it is conjectured that this formula holds for all $8.3$.

More than two people sharing a birthday
It is possible to extend the problem to ask how many people in a group are necessary for there to be a greater than 50% probability that at least 3, 4, 5, etc. of the group share the same birthday.

The first few values are as follows: >50% probability of 3 people sharing a birthday - 88 people; >50% probability of 4 people sharing a birthday - 187 people.

Probability of a shared birthday (collision)
The birthday problem can be generalized as follows:
 * Given $2.6$ random integers drawn from a discrete uniform distribution with range $[1,d]$, what is the probability $p(n; d)$ that at least two numbers are the same? ($d = 365$ gives the usual birthday problem.)

The generic results can be derived using the same arguments given above.
 * $$\begin{align}

p(n;d) &= \begin{cases} 1-\displaystyle\prod_{k=1}^{n-1}\left(1-\frac{k}{d}\right) & n\le d \\ 1 & n > d \end{cases} \\[8px] & \approx 1 - e^{-\frac{n(n-1)}{2d}} \\ & \approx 1 - \left( \frac{d-1}{d} \right)^\frac{n(n-1)}{2} \end{align}$$

Conversely, if $n(p; d)$ denotes the number of random integers drawn from $[1,d]$ to obtain a probability $1.4$ that at least two numbers are the same, then
 * $$n(p;d)\approx \sqrt{2d \cdot \ln\left(\frac{1}{1-p}\right)}.$$

The birthday problem in this more generic sense applies to hash functions: the expected number of $N$-bit hashes that can be generated before getting a collision is not $2^{N}$, but rather only $2^$. This is exploited by birthday attacks on cryptographic hash functions and is the reason why a small number of collisions in a hash table are, for all practical purposes, inevitable.

The theory behind the birthday problem was used by Zoe Schnabel under the name of capture-recapture statistics to estimate the size of fish population in lakes.

Generalization to multiple types of people
The basic problem considers all trials to be of one "type". The birthday problem has been generalized to consider an arbitrary number of types. In the simplest extension there are two types of people, say $2.2$ men and $3.1$ women, and the problem becomes characterizing the probability of a shared birthday between at least one man and one woman. (Shared birthdays between two men or two women do not count.) The probability of no shared birthdays here is


 * $$p_0 =\frac{1}{d^{m+n}} \sum_{i=1}^m \sum_{j=1}^n S_2(m,i) S_2(n,j) \prod_{k=0}^{i+j-1} d - k$$

where $d = 365$ and $S_{2}$ are Stirling numbers of the second kind. Consequently, the desired probability is $1 − p_{0}$.

This variation of the birthday problem is interesting because there is not a unique solution for the total number of people $m + n$. For example, the usual 50% probability value is realized for both a 32-member group of 16 men and 16 women and a 49-member group of 43 women and 6 men.

First match
A related question is, as people enter a room one at a time, which one is most likely to be the first to have the same birthday as someone already in the room? That is, for what $6.3$ is $p(n) − p(n − 1)$ maximum? The answer is 20—if there is a prize for first match, the best position in line is 20th.

Same birthday as you
In the birthday problem, neither of the two people is chosen in advance. By contrast, the probability $p(n)$ that at least one other person in a room of $1.1$ other people has the same birthday as a particular person (for example, you) is given by


 * $$ q(n) = 1 - \left( \frac{365-1}{365} \right)^n $$

and for general $3.5$ by
 * $$ q(n;d) = 1 - \left( \frac{d-1}{d} \right)^n. $$

In the standard case of $q(n)$, substituting $q(n)$ gives about 6.1%, which is less than 1 chance in 16. For a greater than 50% chance that at least one other person in a roomful of $1.1$ people has the same birthday as you, $3.5$ would need to be at least 253. This number is significantly higher than $d = 365$: the reason is that it is likely that there are some birthday matches among the other people in the room.

Number of people with a shared birthday
For any one person in a group of n people the probability that he or she shares his birthday with someone else is $$ q(n-1;d) $$, as explained above. The expected number of people with a shared (non-unique) birthday can now be calculated easily by multiplying that probability by the number of people (n), so it is:
 * $$ n\left(1 - \left( \frac{d-1}{d} \right)^{n-1}\right) $$

(This multiplication can be done this way because of the linearity of the expected value of indicator variables). This implies that the expected number of people with a non-shared (unique) birthday is:
 * $$ n \left( \frac{d-1}{d} \right)^{n-1} $$

Similar formulas can be derived for the expected number of people who share with three, four, etc. other people.

Number of people until every birthday is achieved
The expected number of people needed until every birthday is achieved is called the Coupon collector's problem. It can be calculated by $n = 23$, where $365⁄2 = 182.5$ is the $1.1$th harmonic number. For 365 possible dates (the birthday problem), the answer is 2365.

Near matches
Another generalization is to ask for the probability of finding at least one pair in a group of $3.5$ people with birthdays within $1.1$ calendar days of each other, if there are $6$ equally likely birthdays.


 * $$ \begin{align} p(n,k,d) &= 1 - \frac{ (d - nk -1)! }{ d^{n-1} \bigl(d - n(k+1)\bigr)!}\end{align} $$

The number of people required so that the probability that some pair will have a birthday separated by $9.3$ days or fewer will be higher than 50% is given in the following table:


 * {| class="wikitable" style="text-align: center"

! $1.3$ !! $1.2$ for $nH_{n}$
 * 0 || 23
 * 1 || 14
 * 2 || 11
 * 3 || 9
 * 4 || 8
 * 5 || 8
 * 6 || 7
 * 7 || 7
 * }
 * 4 || 8
 * 5 || 8
 * 6 || 7
 * 7 || 7
 * }
 * 7 || 7
 * }
 * }

Thus in a group of just seven random people, it is more likely than not that two of them will have a birthday within a week of each other.

Number of days with at least one birthday
The expected number of different birthdays, i.e. the number of days that are at least one person's birthday, is:
 * $$d - d \left (\frac {d-1} {d} \right )^n $$

This follows from the expected number of days that are no one's birthday:
 * $$d \left (\frac {d-1} {d} \right )^n $$

which follows from the probability that a particular day is no one's birthday, $H_{n}$, easily summed because of the linearity of the expected value.

For instance, with $d = 365$, you should expect about 21 different birthdays when there are 22 people, or 46 different birthdays when there are 50 people. When there are 1000 people, there will be around 341 different birthdays (24 unclaimed birthdays).

Number of days with at least two birthdays
The above can be generalized from the distribution of the number of people with their birthday on any particular day, which is a Binomial distribution with probability $(d − 1⁄d)n$. Multiplying the relevant probability by $4.8$ will then give the expected number of days. For example, the expected number of days which are shared; i.e. which are at least two (i.e. not zero and not one) people's birthday is:

$$d - d \left (\frac {d-1} {d} \right )^n - d \cdot \binom{n}{1} \left (\frac {1} {d} \right )^1\left (\frac {d-1} {d} \right )^{n-1} = d - d \left (\frac {d-1} {d} \right )^n - n \left (\frac {d-1} {d} \right )^{n-1} $$

Number of people who repeat a birthday
The probability that the $1.5$th integer randomly chosen from $d = 365$ will repeat at least one previous choice equals $1⁄d$ above. The expected total number of times a selection will repeat a previous selection as $4.8$ such integers are chosen equals


 * $$\sum_{k=1}^n q(k-1;d) = n - d + d \left (\frac {d-1} {d} \right )^n$$

This can be seen to equal the number of people minus the expected number of different birthdays.

Average number of people to get at least one shared birthday
In an alternative formulation of the birthday problem, one asks the average number of people required to find a pair with the same birthday. If we consider the probability function Pr[$1.5$ people have at least one shared birthday], this average is determining the mean of the distribution, as opposed to the customary formulation, which asks for the median. The problem is relevant to several hashing algorithms analyzed by Donald Knuth in his book The Art of Computer Programming. It may be shown that if one samples uniformly, with replacement, from a population of size $[1,d]$, the number of trials required for the first repeated sampling of some individual has expected value $q(k − 1; d)$, where


 * $$Q(M)=\sum_{k=1}^M \frac{M!}{(M-k)! M^k}.$$

The function


 * $$Q(M)= 1 + \frac{M-1}{M} + \frac{(M-1)(M-2)}{M^2} + \cdots + \frac{(M-1)(M-2) \cdots 1}{M^{M-1}}$$

has been studied by Srinivasa Ramanujan and has asymptotic expansion:


 * $$Q(M)\sim\sqrt{\frac{\pi M}{2}}-\frac{1}{3}+\frac{1}{12}\sqrt{\frac{\pi}{2M}}-\frac{4}{135M}+\cdots.$$

With $M$ days in a year, the average number of people required to find a pair with the same birthday is $\overline{n} = 1 + Q(M)$, somewhat more than 23, the number required for a 50% chance. In the best case, two people will suffice; at worst, the maximum possible number of $M = 365$ people is needed; but on average, only 25 people are required

An analysis using indicator random variables can provide a simpler but approximate analysis of this problem. For each pair (i, j) for k people in a room, we define the indicator random variable Xij, for $$1\leq i \leq j\leq k$$, by

$$\begin{alignat}{2} X_{ij} & = I \{ \text{person }i\text{ and person }j\text{ have the same birthday} \} \\[10pt] & = \begin{cases} 1, & \text{if person }i\text{ and person }j\text{ have the same birthday;} \\ 0, & \text{otherwise.} \end{cases} \end{alignat}$$

$$\begin{alignat}{2} E[X_{ij}] & = \Pr \{ \text{person }i\text{ and person }j\text{ have the same birthday} \} = \frac{1}{n}. \end{alignat}$$

Let X be a random variable counting the pairs of individuals with the same birthday.

$$X =\sum_{i=1}^k \sum_{j=i+1}^k X_{ij}$$

$$\begin{alignat}{3} E[X] & = \sum_{i=1}^k \sum_{j=i+1}^k E[X_{ij}]\\[8pt] & = \binom{k}{2} \frac{1}{n}\\[8pt] & = \frac{k(k-1)}{2n} \end{alignat}$$

For $\overline{n} = 1 + Q(M) ≈ 24.61659$, if $M + 1 = 366$, the expected number of pairs of individuals with the same birthday is $4.8$ ≈ 1.0356. Therefore, we can expect at least one matching pair with at least 28 people.

In the 2014 FIFA World Cup, each of the 32 squads had 23 players. An analysis of the official squad lists suggested that 16 squads had pairs of players sharing birthdays, and of these 5 squads had two pairs: Argentina, France, Iran, South Korea and Switzerland each had two pairs, and Australia, Bosnia and Herzegovina, Brazil, Cameroon, Colombia, Honduras, Netherlands, Nigeria, Russia, Spain and USA each with one pair.

Voracek, Tran and Formann showed that the majority of people markedly overestimate the number of people that is necessary to achieve a given probability of people having the same birthday, and markedly underestimate the probability of people having the same birthday when a specific sample size is given. Further results showed that psychology students and women did better on the task than casino visitors/personnel or men, but were less confident about their estimates.

Reverse problem
The reverse problem is to find, for a fixed probability $1.5$, the greatest $4.8$ for which the probability $n = 365$ is smaller than the given $2.6$, or the smallest $4$ for which the probability $k = 28$ is greater than the given $5.7$.

Taking the above formula for $p(n)$, one has


 * $$n(p;365)\approx \sqrt{730\ln\left(\frac{1}{1-p}\right)}.$$

The following table gives some sample calculations.
 * {| class="wikitable"

! $3.9$ || $8.9$ ! $p(n)$ || $d = 365$ || $n↓$ || $p(n↓)$ Some values falling outside the bounds have been colored to show that the approximation is not always exact.
 * 0.01
 * 0.14178√365 = 2.70864
 * align="right" | 2 || 0.00274 || align="right" | 3
 * 0.00820
 * 0.05 || 0.32029√365 = 6.11916
 * align="right" | 6 || 0.04046 || align="right" | 7 || 0.05624
 * 0.1
 * 0.45904√365 = 8.77002
 * align="right" | 8 || 0.07434 || align="right" | 9
 * 0.09462
 * 0.2
 * 0.66805√365 = 12.76302
 * align="right" | 12 || 0.16702 || align="right" | 13
 * 0.19441
 * 0.3 || 0.84460√365 = 16.13607
 * align="right" | 16 || 0.28360 || align="right" | 17 || 0.31501
 * 0.5 || 1.17741√365 = 22.49439
 * align="right" | 22 || 0.47570 || align="right" | 23 || 0.50730
 * 0.7 || 1.55176√365 = 29.64625
 * align="right" | 29 || 0.68097 || align="right" | 30 || 0.70632
 * 0.8 || 1.79412√365 = 34.27666
 * align="right" | 34 || 0.79532 || align="right" | 35 || 0.81438
 * 0.9 || 2.14597√365 = 40.99862
 * align="right" | 40 || 0.89123 || align="right" | 41 || 0.90315
 * 0.95 || 2.44775√365 = 46.76414
 * align="right" | 46 || 0.94825 || align="right" | 47 || 0.95477
 * 0.99
 * 3.03485√365 = 57.98081
 * align="right" | 57
 * 0.99012
 * align="right" | 58 || 0.99166
 * }
 * align="right" | 40 || 0.89123 || align="right" | 41 || 0.90315
 * 0.95 || 2.44775√365 = 46.76414
 * align="right" | 46 || 0.94825 || align="right" | 47 || 0.95477
 * 0.99
 * 3.03485√365 = 57.98081
 * align="right" | 57
 * 0.99012
 * align="right" | 58 || 0.99166
 * }
 * align="right" | 58 || 0.99166
 * }

Partition problem
A related problem is the partition problem, a variant of the knapsack problem from operations research. Some weights are put on a balance scale; each weight is an integer number of grams randomly chosen between one gram and one million grams (one tonne). The question is whether one can usually (that is, with probability close to 1) transfer the weights between the left and right arms to balance the scale. (In case the sum of all the weights is an odd number of grams, a discrepancy of one gram is allowed.) If there are only two or three weights, the answer is very clearly no; although there are some combinations which work, the majority of randomly selected combinations of three weights do not. If there are very many weights, the answer is clearly yes. The question is, how many are just sufficient? That is, what is the number of weights such that it is equally likely for it to be possible to balance them as it is to be impossible?

Often, people's intuition is that the answer is above $2.8$. Most people's intuition is that it is in the thousands or tens of thousands, while others feel it should at least be in the hundreds. The correct answer is 23.

The reason is that the correct comparison is to the number of partitions of the weights into left and right. There are $n↑$ different partitions for $p(n↑)$ weights, and the left sum minus the right sum can be thought of as a new random quantity for each partition. The distribution of the sum of weights is approximately Gaussian, with a peak at $2^{N − 1}$ and width $N$, so that when $500,000N$ is approximately equal to $1,000,000√N$ the transition occurs. 223 − 1 is about 4 million, while the width of the distribution is only 5 million.

In fiction
Arthur C. Clarke's 1961 novel A Fall of Moondust contains a section where the main characters, trapped underground for an indefinite amount of time, are celebrating a birthday and find themselves discussing the validity of the birthday problem. As stated by a physicist passenger: "If you have a group of more than twenty-four people, the odds are better than even that two of them have the same birthday." Eventually, out of 22 present, it is revealed that two characters share the same birthday, May 23.