-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbirthdays2_mostly_by_pedro.py
41 lines (29 loc) · 1.21 KB
/
birthdays2_mostly_by_pedro.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 numpy as np
import matplotlib.pyplot as plt
def create_birthdays(students: int) -> list:
# Use random.randint to create a list of birthdays
return list(np.random.randint(1, 366, size=students))
def duplicados(birthdays: list) -> bool:
# Verifique se a lista tem duplicados
# Lembre-se a função set() retorna um objeto sem duplicados
return not len(birthdays) == len(set(birthdays))
def simulations(number_simulations: int, number_students: int) -> float:
# Chame a função create_birthdays e duplicados repetidanente (number) e calcule a probabilidade
out = list()
for i in range(number_simulations):
out += [duplicados(create_birthdays(number_students))]
# print(f'Probability of having two birthdays on the same day is {sum(out)/len(out):.6f}')
return sum(out)/len(out)
def main(n_sim):
ys = list()
for i in range(1, 100):
ys.append(simulations(n_sim, i))
plt.plot(range(1, 100), ys)
plt.xlabel('Número alunos na classe')
plt.ylabel('Probability of having two birthdays on the same day')
plt.show()
if __name__ == '__main__':
n_simulation = 1000
n_students = 67
# simulations(n_simulation, n_students)
main(n_simulation)