import random
from collections import defaultdict
def cut(n, ratio=0.5):
res = []
L = 1
for i in range(n - 1):
x = random.random() * L
res.append(x)
if x >= ratio:
return True
else:
L -= x
last = 1 - sum(res)
res.append(last)
if last >= ratio:
return True
print(res)
return False
def moni(times, n, r):
if times <= 0 or n <= 0:
return 0.0
cnt = 0
for i in range(times):
ret = cut(n, r)
if ret:
cnt += 1
return cnt / times
if __name__ == '__main__':
p = moni(100000, 4, 0.5)
print(p)
网友评论