Monte Carlo Sim的求和结果; Python

发布于 2024-12-13 22:50:23 字数 1082 浏览 1 评论 0原文

在 python 2.7 中工作。

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

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 变量,但我一直无法弄清楚。有什么想法吗?

Working in python 2.7.

I have an argument that takes a list, adds the values of the argument list to the list "team", and then compares certain positional values and returns win, loss, or tie depending upon the values.

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

I want to be able to simulate the results many times, using random.shuffle(team) to randomly order the team list.

I can do that using:

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

But I would like to be able to sum all of the wins, losses and ties from all the simulations, and then divide by the number of simulations (in this case x), to get an average of wins, losses, and ties in all of the simulations.

I've tried changing the starterTrans function to have a total_wins variable that is equal to += wins, but I haven't been able to figure it out. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画▽骨i 2024-12-20 22:50:23

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

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)

i may not getting your point, but...

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

or

    return result['w']/float(x),result['l']/float(x),result['n']/float(x)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文