用Python做數學實驗

來源:互聯網
上載者:User

Determine if you win or loose a hazard game.
Somebody suggests the following game. You pay 1 unit of money and are
allowed to throw four dice. If the sum of the eyes on the dice is less than 9,
you win 10 units of money, otherwise you loose your investment. Should you
play this game?

先用已經知道的數學方法進行求解,假設贏一把的機率是P,求出P即可算出收益的期望。假設4次搖出的骰子正面分別為:x,y,z,w

則勝利的條件是:x+y+z+w<9,其中1<=x,y,z,w<=6

x+y+z+w = 9 時,有C_{8}^{3}種組合;

x+y+z+w = 8 時,有C_{7}^{3}種組合;

x+y+z+w = 7 時,有C_{6}^{3}種組合;

x+y+z+w = 4 時,有1種組合;

所以,P = (C_{8}^{3}+C_{7}^{3}+C_{6}^{3}+C_{5}^{3}+C_{4}^{3}+C_{3}^{3})/64 = 7/72

所以期望為: 7/72*9+65/72*(-1) = –1/36

用Python進行Monte Carlo 模擬,

import randomn = 10000m = 10000money = 0while(n):    b = []    for i in range(1,5):        b.append(random.randint(1,6))    if(sum(b)<9):        money+=9    else:        money-=1    n-=1money = float(money) / mprint money

Python實驗的結果則是:-0.47

雖然都是輸,但是結果有所出入

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.