三赌徒问题,赌徒问题,[Python]代码Py


[Python]代码

Python语言: 三赌徒问题"""http://spellscroll.com/questionfull/4/n=100; simulate(n)"""import randoma = 10; b = 10; c = 10fortune = [a,b,c]def one_round():    p = int(3*random.random())    for i in range(3):        if i==p:            fortune[i] += 2        else:            fortune[i] -= 1def stop():    for i in fortune:        if i==0:            return True    return Falsedef play_game():    global fortune    fortune = [a,b,c]; cnt = 0    while not stop():        one_round(); cnt += 1    return cntdef simulate(n):    cnt = [play_game() for i in range(n)]    sum = reduce(lambda x,y: x+y, cnt, 0)    return sum*1.0/n

评论关闭