Reputation: 2223
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?
Upvotes: 0
Views: 218
Reputation: 6244
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)
Upvotes: 2