2013-12-11 611 views
0

我想通過以下函數進行1000次交互,以確定您是否在此遊戲中獲勝或放棄了金錢。概率骰子Python中的兩個骰子游戲

遊戲的設計是這樣的,你扔一對骰子,並拿回錢或寬鬆的錢。假設我們從5個硬幣開始。

投擲收益率1.5硬幣。

投擲收益率硬幣。

投擲收益率0.5硬幣。

投擲9,8或7不會產生任何結果。

投擲6,5,4,3,2或1扣除0.5硬幣從你的硬幣量。

這是我實現的樣子至今:

def luckCalc(): 

    amount = 5 

    # if 12 then 1/36 chance 

    if random.randrange(1,7) == 6 and random.randrange(1,7) == 6: 
     amount = amount + 1.5 

    # if 11 then 2/36 chance 
    elif (random.randrange(1,7) == 5 and random.randrange(1,7) == 6) or (random.randrange(1,7) == 6 and random.randrange(1,7) == 5): 
     amount = amount + 1 

    # if 10 then 3/36 chance 

    elif (random.randrange(1,7) == 5 and random.randrange(1,7) == 5) or (random.randrange(1,7) == 4 and random.randrange(1,7) == 6) or (random.randrange(1,7) == 6 and random.randrange(1,7) == 4): 
     amount = amount + 0.5 

    # if 9,8,7 
    # 4/36 + 5/36 + 6/36 chance 
    # 1+6, 2+5, 3+4, 4+3, 5+2, 6+1 chance 
    # 2+6, 3+5, 4+4, 5+3, 6+2 chance 
    # 3+6, 4+5, 5+4, 6+3 chance 
    # then no change in amount 

    # if 6,5,4,3,2,1 
    # chances... 
    # then amount -0.5 

return amount 

# Iterate over the dice throwing simulator and calculate total 

total = 0.0 
for a in range(1000): 
    total = total + luckCalc() 

print (total) 

我停止對函數結束編碼,因爲我認識到,必須對如何實現這一目標更優雅的解決方案。任何有趣的建議,我聽說過的蒙特卡羅是什麼?

+4

您當前的常規「重新卷」的骰子每個'if'聲明。以'roll = random.randrange(1,7)+ random.rangerange(1,7)'開始,然後對其進行測試。您每次通過luckCalc重設金額,而不是跟蹤外部總額。另外,11做什麼? – jonrsharpe

+0

幹得好,你敏銳的眼睛發現了一個邏輯漏洞。 - 我添加了11. – nottinhill

回答

2

每當您撥打random.randrange(1,7)時,您都會生成一個新的隨機數。既然你正在測試一個「轉」,推出兩次:

def roll_die(): 
    return random.randrange(1, 7) 

total = roll_die() + roll_die() 

,看看總和範圍:

def play_turn(): 
    total = roll_die() + roll_die() 

    if total == 12: 
     return 1.5 
    elif total == 11: 
     return 1.0 
    elif total == 10: 
     return 0.5 
    elif total <= 6: 
     return -0.5 
    else: # total is 7, 8, or 9 
     return 0 

這裏有10萬髮結果:

>>> from collections import Counter 
>>> counts = Counter(play_turn() for i in xrange(100000)) 
>>> counts 
    Counter({-0.5: 41823, 0: 41545, 0.5: 8361, 1.0: 5521, 1.5: 2750}) 
>>> probabilities = {score: count/100000.0 for score, count in counts.items()} 
>>> probabilities 
    {-0.5: 0.41823, 0: 0.41545, 0.5: 0.08361, 1.0: 0.05521, 1.5: 0.0275} 
0

我注意到你的代碼中有一些東西。首先,對於6-1的情況,你實際上並未從金額中減去0.5。其次,由於你沒有在每個循環中傳遞最初的數量,所以你的總和增加了5到6.5,這使總數變得毫無意義。

一個更有效的總量將在量每次經過:

def luckCalc(amount): 

然後換你的循環:

total = 5.0 
for a in range(1000): 
    total = luckCalc(total) 

Blender的答案,它剛剛發佈,我在寫這一點,是簡化主要功能的好方法。

+0

總數通過外迭代得到正確計算。在luckCalc函數中,我添加了僞代碼並將其註釋掉。你的例子也會通過1000作爲硬幣的起始金額,這不符合我提供的規格。 – nottinhill

+0

猜猜我誤解你的遊戲。正如你寫的那樣,你每輪都會憑空加入5枚新硬幣。我以爲你只想從5個硬幣開始,看看你在1000輪後會有多少錢。 – user3058428

0

我個人喜歡把我的結果表設置爲一個數組(或字典,但這更適合我的目的,因爲每個結果都是少量可能的整數之一),每個骰子的索引設置爲由此產生的變化的價值。見下文。

import random 

def luckCalc(coins=5): 

    diceroll = random.randint(1,6)+random.randint(1,6) #roll them bones 

    #here's that table I was talking about.... 
    results_table = ['index 0 is blank',"you can't roll a one on two dice",-.5,-.5,-.5,-.5,-.5,0,0,0,.5,1,1.5] 

    coins += results_table[diceroll] #changes your coins value based on your roll (as an index of results_table) 

    if results_table[diceroll] > 0: #change the string if your result was + or - 
    result = "gained {}".format(results_table[diceroll]) 
    else: 
    result = "lost {}".format(results_table[diceroll]*-1) 

    print("You {} coins, putting you at {}".format(result,coins)) #report back to the user 
    return coins #this is how you save your output 


#CONSTANTS GO HERE -- YOU CAN CHANGE THESE TO CHANGE YOUR PROGRAM 
STARTING_COINS = 5 
HOW_MANY_ITERATIONS = 1000 

#this way we don't modify a constant 
coins = STARTING_COINS 

#do it how many times? 
for _ in range(HOW_MANY_ITERATIONS): #oh yeah that many times 
    coins = luckCalc(coins) #runs the function and saves the result back to coins 

#report to the user your final result. 
print("After {} rolls, your final total is {}".format(HOW_MANY_ITERATIONS,coins)) 
2

實際上,你可以滾(哈哈!)你正在做成一個單一的功能,一切:

from random import randrange 

def play_game(rolls=1000, amount=5, n=6): 
    """Play game 'rolls' times, starting with 'amount' on 'n'-sided dice.""" 
    for i in range(rolls): 
     roll = randrange(1, n+1) + randrange(1, n+1) 
     if roll == 12: 
      amount += 1.5 
     elif roll == 11: 
      amount += 1 
     elif roll == 10: 
      amount += 0.5 
     elif roll < 7: 
      amount -= 0.5 
    return amount 
+0

我很喜歡你的答案,因爲它的多功能性,在我的問題下你的快速評論是第一位的,但是Blender做了很好的解釋。 – nottinhill