
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Birthday Paradox in Python
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 364365
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×⟮1−1365⟯×⟮1−2365⟯×⟮1−3365⟯×⟮1−4365⟯...
Thus,
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.
Example
import math def findPeople(p): return math.ceil(math.sqrt(2*365*math.log(1/(1-p)))) print(findPeople(0.70))
Output
Running the above code will generate the output as,
30