def simNGame(probA, probB, n):
# simulate n games
# prob is the chance to win of person A or person B, n is the times of games
# return the number of wins of A and B
winsA = 0
winsB = 0
for i in range(n):
scoreA, scoreB = simOneGame(probA, probB)
if scoreA > scoreB:
winsA += 1
else:
winsB += 1
return winsA, winsB
def simOneGame(probA, probB):
# simulate 1 game
# return the score of A and B
scoreA = 0
scoreB = 0
serving = 'A'
while not gameOver(scoreA, scoreB):
if serving == 'A':
if probA > random():
scoreA += 1
else:
serving = 'B'
else:
if probB > random():
scoreB += 1
else:
serving = 'A'
return scoreA, scoreB
def gameOver(scoreA, scoreB):
# check the game is over or not
return scoreA == 15 or scoreB == 15
This code is used to simulated the racquetaball. when I ran it with two people with same changce to win, I noticed that winsB was always 4700+ and winsA was always 5200+, so I try this:
while True:
a, b = simNGame(0.5, 0.5, 10000)
print a > b
All of the results is True, but I cannot check any errors in my code.why,thanks
Aucun commentaire:
Enregistrer un commentaire