Create a gist now

Instantly share code, notes, and snippets.

@Henoc /cw.py
Created Nov 22, 2015

What would you like to do?
最大連勝数の期待値
# モンテカルロ法で最大連勝数の期待値を出す
# x : 試合数
# p : 勝率
# n : モンテカルロを試行する回数
import random
def get_max_continuous_true(lst):
"Boolean List を受け取り, Trueの最大連続数を返す"
cnt = 0
max_cnt = 0
for e in lst:
if e:
cnt += 1
else:
max_cnt = cnt if cnt > max_cnt else max_cnt
cnt = 0
max_cnt = cnt if cnt > max_cnt else max_cnt
return max_cnt
p = 0.6
n = 10000
print("| x | E(x) |")
print("|----:|---------:|")
for x in range(10,300,10):
ct_sum = 0
for i in range(n):
scores = [(random.random() < p) for i in range(x)]
ct_sum += get_max_continuous_true(scores)
ans = 1.0 * ct_sum / n
print("|{0:>5}|{1:>10}|".format(x,ans))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment