【问题标题】:Summing result of Monte Carlo Sim; pythonMonte Carlo Sim 求和结果; Python
【发布时间】:2011-12-22 11:49:41
【问题描述】:

在 python 2.7 中工作。

我有一个参数,它接受一个列表,将参数列表的值添加到列表“团队”中,然后比较某些位置值并根据值返回胜利、失败或平局。

def starterTrans3(starter):
    wins = 0
    losses = 0
    nd = 0
    team = [[1, 0], [1, 0], [0, 5], [3, -1]]
    random.shuffle(team)
    for t, s in zip(team, starter):
        t.extend(s)
    score_add(team, exit_score(team, starter))
    length = len(starter)
    for i in range(0, length):
        if team[i][4] > 0 and (team[i][1] > -team[i][4]) and team[i][2] >= 5:
            wins += 1
        elif team[i][4] < 0 and (team[i][1] <= -team[i][4]):
            losses += 1
        elif (team[i][4] <= 0 and team[i][1] >= -team[i][4]):
            nd += 1
    return wins, losses, nd

我希望能够多次模拟结果,使用 random.shuffle(team) 对团队列表进行随机排序。

我可以这样做:

def MonteCarlo(starter, x):
    for i in range(0, x):
        print starterTrans3(starter)

但我希望能够将所有模拟的所有赢、输和平局相加,然后除以模拟次数(在本例中为 x),得到赢、输和在所有模拟中都有联系。

我尝试将 starterTrans 函数更改为具有等于 += wins 的 total_wins 变量,但我无法弄清楚。有什么想法吗?

【问题讨论】:

    标签: python for-loop sum montecarlo


    【解决方案1】:

    我可能不明白你的意思,但是...

    def MonteCarlo(starter, x):
        result = dict(w=0,l=0,n=0)
        for i in range(0, x):
            w,l,n = starterTrans3(starter)
            result['w']+=w
            result['l']+=l
            result['n']+=n
        return result
    

    或

        return result['w']/float(x),result['l']/float(x),result['n']/float(x)
    

    【讨论】:

      猜你喜欢
      • 2011-12-22
      • 1970-01-01
      • 1970-01-01
      • 2016-06-24
      • 2019-05-09
      • 1970-01-01
      • 2019-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多