我在Python中使用隨機模擬骰子游戲。然後我模擬遊戲的次數,看看玩家多久擊敗經銷商。我有一個帶有種子的測試文件來檢查我的代碼,但是我的數字略有偏差。我認爲錯誤在於結構,但似乎無法弄清楚究竟是什麼。模擬骰子游戲1000次
骰子輥
def quietRoll():
return random.randrange(1,7) + random.randrange(1,7)
骰子模擬
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
運行擲骰子的n倍量
def testCraps(n):
count = 0
playerWin = 0
while count <= n:
if quietCraps() == 1:
playerWin += 1
count += 1
else:
count += 1
return playerWin/n
預計輸出
Failed example:
random.seed(5)
testCraps(1000)
Expected:
0.497
Got:
0.414
而你的問題是...什麼? –
@DavidSchwartz什麼導致期望的輸出與實際不同? – 23k
你是怎麼想出你的預期產量的? – 2015-11-05 23:36:39