我需要这段代码来输出匹配生日的总数,但计数一直在增长,我不知道为什么。我们应该期望看到总数约为。模拟量的一半。我现在将它附加到一个列表中,以可视化计数的变化。
import random
birthmonth = []
birthday= []
def birthday_paradox():
"""
"""
months31 = (1,3,5,7,8,10,12)
months30 = (4,6,9,11)
day31 = random.randint(1,31)
day30 = random.randint(1,30)
day29 = random.randint(1,29)
month = random.randint(1,12)
for i in months31:
if month == i:
birthmonth.append(month)
birthday.append(day31)
for i in months30:
if month == i:
birthmonth.append(month)
birthday.append(day30)
if month == 2:
birthmonth.append(month)
birthday.append(day29)
def frequency_of_matches(number_of_people:int = 23):
"""
"""
count = 0
for i in range(number_of_people):
birthday_paradox()
sorted_birthday_list = sorted(list(zip(birthmonth, birthday)))
for i in range(len(sorted_birthday_list)):
if sorted_birthday_list[i] == sorted_birthday_list[i - 1]:
count += 1
return count
def simulations(number_of_simulations:int = 100):
total = []
for i in range(number_of_simulations):
total.append(frequency_of_matches())
return total
simulations(25)
示例输出:
[1, 3, 7, 9, 13, 23, 28, 42, 51, 61, 70, 84, 97, 112, 124, 138, 153, 163, 178, 196, 213, 228, 244, 261, 277]