I'm using random in Python to simulate a game of craps. I'm then simulating the game n amount of times to see how often the player beats the dealer. I have a test file with seeds to check my code in, but my numbers are just slightly off. I think the mistake is in the structure, but can't seem to figure out what exactly it is.
The dice roll
def quietRoll():
return random.randrange(1,7) + random.randrange(1,7)
Craps simulation
def quietCraps():
#first roll
firstRoll = quietRoll()
if firstRoll in (7,11):
return 1
elif firstRoll in (2,3,12):
return 0
#every other roll
newDice = quietRoll()
while newDice not in (7, firstRoll):
newDice = quietRoll()
if newDice == firstRoll:
return 1
if newDice == 7:
return 0
Running craps n amount of times
def testCraps(n):
count = 0
playerWin = 0
while count <= n:
if quietCraps() == 1:
playerWin += 1
count += 1
else:
count += 1
return playerWin/n
Expected output
Failed example:
random.seed(5)
testCraps(1000)
Expected:
0.497
Got:
0.414
Aucun commentaire:
Enregistrer un commentaire