概率问题(python实现)
程序员文章站
2022-09-13 22:35:13
两只球队,每支球队11人加裁判共23人,假设都是同年出生,那么至少有两人在同一天出生的概率是多少?import randomn=0def dataint(down,up,k): """ 产生多维随机整数 :param down: :param up: :param k: :return: """ data=[] for i in range(k): temp = random.randint(down, up)...
两只球队,每支球队11人加裁判共23人,假设都是同年出生,那么至少有两人在同一天出生的概率是多少?
import random
def dataint(down,up,k): #产生多维随机数
data=[]
for i in range(k):
temp = random.randint(down, up)
data.append(temp)
return data
n=0
for m in range(1,10000): #测试一万组数据
data_1 = dataint(1,365,23) #随机生成一个数为23且每个数介于1到365的列表
for i in range(0,22):
for j in range(i+1,22): #两两比较相同,有相同则n加1
if data_1[i] == data_1[j]:
n=n+1
break_flag=True
print(data_1)
break
if break_flag==True:
break
print(n , float(n)/10000) #得出概率
不过生成的结果却与浙大概率视频笔算得出的结果相差一个百分点,按照古典概率方法计算应该是50%左右。
...............................
[150, 353, 4, 350, 330, 297, 348, 82, 18, 301, 281, 69, 23, 145, 213, 288, 150, 30, 17, 101, 204, 175, 363]
[201, 353, 362, 185, 18, 261, 13, 280, 283, 303, 318, 235, 138, 95, 220, 201, 59, 167, 315, 311, 313, 183, 182]
[192, 318, 33, 64, 20, 234, 341, 184, 316, 301, 67, 139, 317, 358, 192, 13, 325, 364, 259, 82, 247, 175, 50]
[205, 197, 126, 201, 82, 236, 59, 17, 304, 327, 325, 153, 268, 270, 156, 5, 205, 62, 257, 132, 363, 19, 355]
[119, 24, 125, 119, 209, 170, 208, 65, 186, 166, 313, 279, 271, 135, 290, 252, 35, 356, 84, 359, 108, 172, 272]
[188, 206, 18, 289, 186, 259, 216, 130, 287, 243, 6, 108, 184, 304, 87, 81, 188, 227, 7, 196, 213, 331, 163]
[171, 247, 171, 329, 70, 92, 125, 176, 71, 193, 31, 25, 91, 165, 101, 351, 4, 302, 49, 254, 218, 20, 206]
561 0.0561
不解,劳烦各位大哥加以指教
本文地址:https://blog.csdn.net/qq_35526921/article/details/107141639