您的位置:首页 > 其它

伯努力抛硬币实验连续出现n个正面的概率

2017-10-31 17:14 519 查看
1. 源代码 (math.py)

def p(m,n):
if m < n:
return 0
elif (m, n) in results:
return results[(m, n)]
else:
prob = 0
for i in range(n-1, 0, -1):
prob += p(m-i,n)*.5**i
prob += .5 ** (n-1)
results[(m, n)] = prob
return prob

M = 7
results = {}
for N in range(1,8):
prob = p(M,N)
print('%f'%(prob))
print('M=%d, N=%d, prob is %f'%(M, N, prob))


参考

http://blog.csdn.net/m0_37786651/article/details/62437291
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: