-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathprobability.py
83 lines (76 loc) · 2.96 KB
/
probability.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
"""
In data science, probability is often used to simulate
scenarios. The code on the right simulates the birthday
problem. Right now the code simulates a room with only
2 people that get random birthdays, and the probability
that those 2 people have the same birthday is really low.
Change the number 2 to a higher number of your choosing
where it says #Change This Number and run the code.
Is there a match in the simulation? What’s the probability
that there would be a match? Keep changing the number
to test out different simulations.
"""
import random
num_people_in_room = 100 #Change This Number
#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
simulate(num_people_in_room)