Birthday Paradox in Python

PythonServer Side ProgrammingProgramming

The birthday paradox is a very famous problem in the section of probability.

Problem Statement − There are several people at a birthday party, some are having the same birthday collision. We need to find the approximate number of people at a birthday party on the basis of having the same birthday.

In the probability, we know that the chance of getting ahead is 1/2, same as if we have some coins, the chance of getting 10 heads is 1/100 or 0.001.

Let us understand the concept.

The chance of two people having the different birthday is $$\frac{364}{365}$$ which is $$\lgroup1-\frac{1}{365}\rgroup$$ in a Non-leap year.

Thus, we can say that the first person having the probability of a specific birthday is ‘1’ and for others, it would be different which is,

P(different) = $$1\times\lgroup1-\frac{1}{365}\rgroup\times\lgroup1-\frac{2}{365}\rgroup\times\lgroup1-\frac{3}{365}\rgroup\times\lgroup1-\frac{4}{365}\rgroup...$$


P(same) = 1 − P(different)

For example, the number of people having the same birthday for which probability is 0.70.

N = √2 × 365 × log(1-1/p)

N = √2 × 365 × log(1-1/0.70) = 30

Thus, the total approximate no. of people having the same birthday is 30.


 Live Demo

import math
def findPeople(p):
   return math.ceil(math.sqrt(2*365*math.log(1/(1-p))))


Running the above code will generate the output as,

Published on 05-Feb-2021 07:01:07