python数学小实验(5)——掷硬币出现连续正面的概率
连续扔100次硬币,出现连续正面或连续反面大于n次的概率,例如n=5时,有多大的概率?
import numpy as np
NUM_EXER = 10000
count_max = 0
for i in range(NUM_EXER):
L = []
for i in range(100): #throw coins
if np.random.rand() > 0.5:
L.append(1)
else:
L.append(0)
C = []
count = 1
L.append(-1) #sentinel node
for i in range(len(L)-1):
if L[i+1] == L[i]:
count +=1
else:
if count>1:
C.append(count)
count = 1
if np.max(C) >= 5:
count_max +=1
print(count_max/NUM_EXER)
输出:0.9727
进一步地,可以获得连续出现次数的概率分布情况。