-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbirthday_paradox.py
49 lines (31 loc) · 930 Bytes
/
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
from random import *
def has_duplicates(t):
for x in t:
if t.count(x) > 1:
return True
# print(has_duplicates(['a', 'e', 'b', 'l', 'l', 'l']))
def get_month():
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
rm = choice(months)
return rm
def generate_birthday():
bdl = []
for i in range(23):
mon = get_month()
if mon != 'Feb':
bdl.append(mon + ' ' + str(randint(1, 32)))
else:
bdl.append(mon + ' ' + str(randint(1, 30)))
return bdl
def calc_probability_birthday_paradox():
pbp = []
for i in range(0, 1000):
t = str(has_duplicates(generate_birthday()))
pbp.append(t)
for x in pbp:
if x == 'True':
return pbp.count(x)/1000
total = 0
for i in range(50):
total += float(calc_probability_birthday_paradox())
print(total/50)