[英]monte carlo of a preexisting program
diceGame.py(以下)中的python代碼包含我們在課堂上討論過的骰子游戲的python實現。 假設我們運行了N次代碼,並根據A發生的次數除以N來估計某個事件A的概率P r {A}。然后我們重復此過程M次,收集概率pi,i = 1 ,。 。 。 ,M。假設兩個骰子都是公平的,請針對以下概率使用N = 10、100、1000和M = 100修改diceGame.py的主例程:
在第一個擲骰子中獲勝的概率。
如果第一輪擲出4,則贏得游戲的可能性。
贏得比賽的概率。
游戲要求擲骰子超過5次的概率。
我知道所有這些答案都可以通過代碼本身輕松提供。 我的問題是我不知道如何編輯python代碼以提供這些所需的輸出。
這是代碼:
# ===============================
# IMPORTS RANDOM NUMBER GENERATOR
# ===============================
import random
# ================================================================
# GENERATES RANDOMLY THE SUM OF TWO INTEGERS IN THE [1,6] INTERVAL
# ================================================================
def rollDices():
return int(random.randint(1,6) + random.randint(1,6))
# ================================================
# RETURN THE OUTCOME STRING GIVEN AN INTEGER STATE
# ================================================
def getOutcome(outcome):
if(outcome == 0):
result = "Lost at first roll."
elif(outcome == 1):
result = "Won at first roll."
elif(outcome == 2):
result = "Won on [4,5,6,8,9,10]"
elif(outcome == 3):
result = "Lost on [4,5,6,8,9,10]"
return result
# ==============
# PLAYS THE GAME
# ==============
def playGame():
# Define Variables
gameFinished = False
wonGame = False
totRolls = 0
while (not(gameFinished)):
# ROLL DICES
totScore = rollDices()
totRolls += 1;
# GAME IS LOST
if(totScore in [2,3,12]):
gameFinished = True
wonGame = False
return 0,wonGame,totRolls,totScore
# GAME IS WON
if(totScore in [7,11]):
gameFinished = True
wonGame = True
return 1,wonGame,totRolls,totScore
# JUST CONTINUE PLAYING
if(totScore in [4,5,6,8,9,10]):
# REPEAT UNTIL YOU FIND THE SCORE AGAIN OR 7
newScore = 0
while((newScore != totScore)and(newScore != 7)):
newScore = rollDices()
totRolls += 1;
# CHECK IF YOU WIN OR LOOSE
if(newScore == totScore):
gameFinished = True
wonGame = True
return 2,wonGame,totRolls,totScore
if(newScore == 7):
gameFinished = True
wonGame = False
return 3,wonGame,totRolls,totScore
# ============
# MAIN PROGRAM
# ============
if __name__ == "__main__":
intVal,wonGame,numRolls,lastScore = playGame()
print("Outcome: %s" % (getOutcome(intVal)))
if(wonGame):
print("You have won the game.")
else:
print("You have lost the game.")
print("Total rolls needed for this game: %d" % (numRolls))
print("Last outcome: %d" % (lastScore))
我嘗試問題時的新主要功能:
if __name__ == "__main__":
m = 100
n = 10
FRwins = 0
for i in range(m):
for j in range(n):
intVal,wonGame,numRolls,lastScore = playGame()
if getOutcome(intVal) == 1:
FRwins += 1
FRwins = FRwins/float(n)
print(FRwins)
目前,該程序正在玩一次游戲。 如果您看最后10行代碼,這就是您要進行調整的地方。 重要的一行是這樣的:
intVal,wonGame,numRolls,lastScore = playGame()
它僅被調用一次,因此您只能從中得到一個結果。 由於您需要進行蒙特卡洛模擬(即一遍又一遍地播放並跟蹤概率),因此只需要重播此行很多次,並每次都跟蹤結果即可。
從那里,您將需要設置一些for循環,例如:
for i in range(m):
並跟蹤每次模擬的結果。 由於您正在使用m和n,因此您將需要在playGame()周圍使用嵌套循環。 然后,您只需要隨時計算結果,然后除以嘗試獲得概率的總嘗試次數即可。
希望這會有所幫助。 隨意提出其他問題-我試圖使它足夠籠統,以至於我實際上並未編寫似乎是您的作業的代碼!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.