Probability of distinct birthdays in a group

Given n people in a group, what is the probability that each of them have a distinct birthday (ignoring the case of a leap year and assuming that an year has 365 days).

The analytical answer is $$\frac{365/(365-n)!}{365^{n}}$$


In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from math import factorial
from decimal import Decimal

In [2]:
def permutation(n, k):
    return Decimal(factorial(n))/Decimal(factorial(n-k))

In [3]:
def probability_of_distinct_birthdays(n):
    return permutation(365, n)/Decimal(365**n)

In [4]:
n = range(1, 365)
prob = map(probability_of_distinct_birthdays, n)

In [5]:
plt.plot(n, prob)
plt.grid()



In [6]:
plt.semilogy(n, prob)
plt.grid()



In [7]:
for i, iprob in enumerate(prob):
    if iprob < 0.5:
        print i+1
        break


23

So, even for a group as small as 23 people, there is a 50% chance that two people will share the same birthday!