-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulate.py
71 lines (65 loc) · 2.59 KB
/
simulate.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
import random
# Simulate a room with a certain number of people
def simulate(num_people):
birthdays = []
print("Here's what our room looks like:\n")
months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October",
"November", "December"]
# Assign a random birthday to each person
for i in range(0, num_people):
# Choose a random month
month_choice = random.choice(months)
# Choose a random day based on month
if month_choice == "February":
day = random.randint(1, 29)
elif month_choice == "April" or month_choice == "June" or month_choice == "September" or month_choice == "November":
day = random.randint(1, 30)
else:
day = random.randint(1, 31)
birthday = month_choice + " " + str(day)
# Store the birthday
birthdays.append(birthday)
print("Person {0}'s birthday: {1}".format(i + 1, birthday))
calculate_probability(num_people)
match = False
# Check for matching birthdays
for i in range(len(birthdays)):
if find_duplicates(birthdays, birthdays[i], i):
match = True
break
if not match:
print("\n\nIn our simulation, no two people have the same birthday")
# Calculate the probability of there being 2 people with the same birthday
def calculate_probability(num_people):
# Check there is at least 2 people in the room
if num_people < 2:
print("\n\nNot enough people in the room!")
return
else:
# Calculate the probability
numerator = 365
countdown = 364
for i in range(2, num_people + 1):
numerator = numerator * countdown
countdown -= 1
denominator = 365 ** num_people
probability = 1 - numerator / float(denominator)
# Change probability to percentage
rounded = round(probability * 100, 2)
print(
"\n\nThe probability that two people in a room of {0} people have the same birthday is nearly {1}%".format(
num_people, rounded))
# Find the same birthday within our list of birthdays
def find_duplicates(birthdays_list, birthday, index):
people = []
for i in range(len(birthdays_list)):
if birthdays_list[i] == birthday and i != index:
people.append(i + 1)
if people:
people.append(index + 1)
print("\n\nIn our simulation, the following people have the same birthdays: ")
for person in people:
print("Person {0}".format(person))
return True
else:
return False