python_for_asl/Exercise/exercise-5.2.py
2025-08-06 02:04:35 +08:00

23 lines
669 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import random
# 概率计算表明23个人中有至少两人生日相同的概率高达50.7%,听起来有些反直觉,我们来通过计算估计这一概率。
def contains_same_birthday(birthdays):
# 修改代码判断birthdays这个list中是否有相同的元素
return True
# 我们进行10000次测试
test_count = 10000
# 有至少两人生日相同的次数
true_count = 0
for i in range(test_count):
birth_days = []
for j in range(23):
birth_days.append(random.randint(1, 365))
# 统计满足条件的测试次数
# 输出测试结果保留4位小数
print('23个人中有至少两人生日相同的概率是:')