编写程序随机产生23个人的生日列表,生日的编号可以简单地认 为是从1到365中的任一整数(不需要用年月日表示)。重复100000次,记录下这100000次中存在两个(或两个以上)人的生日相同的次数。
时间: 2023-05-14 11:05:19 浏览: 63
这是一个经典的生日悖论问题,可以使用概率统计的方法来解决。以下是一个可能的解决方案:
```python
import random
def generate_birthday_list(n):
return [random.randint(1, 365) for i in range(n)]
def has_duplicate(lst):
return len(lst) != len(set(lst))
def simulate(num_trials, num_people):
count = 0
for i in range(num_trials):
birthdays = generate_birthday_list(num_people)
if has_duplicate(birthdays):
count += 1
return count
num_trials = 100000
num_people = 23
count = simulate(num_trials, num_people)
print("Number of trials:", num_trials)
print("Number of people:", num_people)
print("Number of trials with duplicate birthdays:", count)
```
输出结果可能如下:
```
Number of trials: 100000
Number of people: 23
Number of trials with duplicate birthdays: 50728
```
这意味着在重复100000次的实验中,有50728次出现了至少两个人生日相同的情况。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)