看看python是如何解决三赌徒问题的,python赌徒,关于Python语言的三


关于Python语言的三赌徒问题是如何解决的呢?就来看看下面这段关于python是如何解决三赌徒问题的源代码吧。(供参考!)

Python random模块使用方法。

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 cnt#www.iplaypy.comdef simulate(n):    cnt = [play_game() for i in range(n)]    sum = reduce(lambda x,y: x+y, cnt, 0)    return sum*1.0/n

编橙之家文章,

评论关闭