-
Notifications
You must be signed in to change notification settings - Fork 0
/
birthday.py
40 lines (33 loc) · 1.09 KB
/
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
30
31
32
33
34
35
36
37
38
39
40
import random
def has_duplicates(t):
"""Returns True if any element appears more than once in (t),
False otherwise."""
s = t[:]
s.sort()
for i in range(len(s)-1):
if s[i] == s[i+1]:
return True
return False
def random_bdays(n):
"""Returns a list of integers between 1 and 365, with length (n)."""
t = []
for i in range(n):
bday = random.randint(1, 365)
t.append(bday)
return t
def count_matches(students, samples):
"""Generates (samples) samples of (students) students, and counts
how many of them have at least one pair of students with the same bday."""
count = 0
for i in range(samples):
t = random_bdays(students)
if has_duplicates(t):
count += 1
return count
"""run the birthday simulation 1000 times and print the number of matches"""
num_students = 23
num_simulations = 1000
count = count_matches(num_students, num_simulations)
print('After %d simulations' % num_simulations)
print('with %d students' % num_students)
print('there were %d simulations with at least one match' % count)