-
Notifications
You must be signed in to change notification settings - Fork 0
/
birthday.py
30 lines (24 loc) · 830 Bytes
/
birthday.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import argparse
import sys
import random
DAYS_PER_YEAR=365
def random_birthday():
return random.randrange(0,DAYS_PER_YEAR)
def birthdayExperiment():
peopleWithBirthday = [False] * DAYS_PER_YEAR
nb_persons = 0
while True:
nb_persons += 1
person = random_birthday()
if peopleWithBirthday[person]:
return nb_persons
peopleWithBirthday[person] = True
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Estimate chance of two people having same birthday')
parser.add_argument('nb_trials', type=int, help='number of times the experiment should be run')
args = parser.parse_args()
nb_trials = args.nb_trials
nb_persons = 0
for i in range(nb_trials):
nb_persons += birthdayExperiment()
print(nb_persons/nb_trials)