-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbirthday_paradox.py
83 lines (67 loc) · 2.65 KB
/
birthday_paradox.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import datetime, random
def getBirthdays(numberOfBirthdays):
birthdays = []
for i in range(numberOfBirthdays):
startOfYear = datetime.date(2001, 1, 1)
randomNumberOfDays = datetime.timedelta(random.randint(0, 364))
birthday = startOfYear + randomNumberOfDays
birthdays.append(birthday)
return birthdays
def getMatch(birthdays):
if len(birthdays) == len(set(birthdays)):
return None
for a, birthdayA in enumerate(birthdays):
for b, birthdayB in enumerate(birthdays[a + 1:]):
if birthdayA == birthdayB:
return birthdayA
MONTHS = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')
while True:
print('How many birthdays shall I generate? (Max 100)')
response = input('> ')
if response.isdecimal() and (0 < int(response) <= 100):
numBDays = int(response)
break
print()
print('Here are', numBDays, 'birthdays:')
birthdays = getBirthdays(numBDays)
for i, birthday in enumerate(birthdays):
if i != 0:
print(', ', end='')
monthName = MONTHS[birthday.month - 1]
dateText = '{} {}'.format(monthName, birthday.day)
print(dateText, end='')
print()
print()
match = getMatch(birthdays)
print('In this simulation, ', end='')
if match != None:
monthName = MONTHS[match.month - 1]
dateText = '{} {}'.format(monthName, match.day)
print('multiple people have a birthday on', dateText)
else:
print('there are no matching birthdays.')
print()
print('Generating', numBDays, 'random birthdays 100, 000 times...')
input('Press Enter to begin...')
print('Let\'s run another 100, 000 simulations.')
simMatch = 0
for i in range(100000):
if i % 10000 == 0:
print(i, 'simulations run...')
birthdays = getBirthdays(numBDays)
if getMatch(birthdays) != None:
simMatch = simMatch + 1
print('100,000 simulations run.')
probability = round(simMatch / 100000 * 100, 2)
print('Out of 100,000 simulations of', numBDays, 'people, there was a')
print('matching birthday in that group', simMatch, 'times. This means')
print('that', numBDays, 'people have a', probability, '% chance of')
print('having a matching birthday in their group.')
''' EXPLORE THE PROGRAM
What error message do you get if you delete or comment out numBDays = int(response)?
NameError: name 'numBDays' is not defined
How can you make the program display full month names, such as 'January' instead of 'Jan'?
Change the output in the MONTHS tuple
How could you make 'X simulations run...' appear every 1,000 simulations instead of every 10,000?
Change the number after the modulo operator on line 60 to 1000
'''