How many times the coin toss, two up front to appear?
The problem lies:
https://zhuanlan.zhihu.com/p/68358814
The core problem is the formula:
Currently there have been continuous times face up, and only a thin last step. First, at least equal to
, Because I have been test times, this is the first that is right. Then the next step (I call it the key step) There are two cases: if it is face up, then it's done, because the probability is face-up
, so it corresponds to the second term of the right side of the equation; if it is negative up, then fall short, start all over again, which corresponds to the third term of the right side of the equation. Finally get the desired 6
Python simulation as follows:
import numpy as np
def TossCoin(Consecutive_num):
temp = 0 #记录连续次数
count = 0 #记录掷硬币次数
while temp < Consecutive_num+1:
count += 1
result = np.random.binomial(1,0.5)
if result == 1:
temp += 1
else:
temp = 0
if temp == Consecutive_num:
break
return count
TossCoin_result = []
for i in range(100000):
TossCoin_result.append(TossCoin(2))
print(np.mean(TossCoin_result))
Output: 6.00694
experiment 3 times, as output: 259.2
According to the calculation
Supplementary article: https://www.cnblogs.com/avril/archive/2013/06/28/3161669.html