Created
April 7, 2015 19:12
-
-
Save raymonstah/9e0d43bbb281fc8957c7 to your computer and use it in GitHub Desktop.
Birthday paradox / problem. In a room of 70 people, there's a 99.9% chance that there will be a pair with the exact same day of birth.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In probability theory, the birthday paradox concerns the probability that, | |
# in a set of n randomly chosen people, | |
# some pair of them will have the same birthday. | |
# By the pigeonhole principle, the probability reaches 100% when | |
# the number of people reaches 367. | |
import random | |
# 365 distinct birthdays including February 29th. | |
birthdays = 365 | |
# Returns true if two people out of n_people have the same birthday. | |
def birthday(n_people): | |
distinct_dates = set() | |
for person in range(n_people): | |
birthdate = random.randrange(birthdays + 1) | |
if birthdate in distinct_dates: | |
return True | |
else: | |
distinct_dates.add(birthdate) | |
return False | |
def simulations(n_people, trials): | |
successes = 0 | |
for trial in range(trials): | |
if birthday(n_people): | |
successes += 1 | |
return successes/trials | |
if __name__ == '__main__': | |
print(simulations(23, 10000)) # 50% chance with only 23 people. | |
print(simulations(70, 10000)) # 99.9% chance with 70 people. | |
print(simulations(366, 10000)) # 100% because pigeonholed. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment