• 用Python做数学实验


    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 random
    n = 10000
    m = 10000
    money = 0
    while(n):
        b = []
        for i in range(1,5):
            b.append(random.randint(1,6))
        if(sum(b)<9):
            money+=9
        else:
            money-=1
        n-=1
    money = float(money) / m
    print money
    

    Python实验的结果则是:-0.47

    虽然都是输,但是结果有所出入

  • 相关阅读:
    二分查找 java代码
    Failed at the bitcore-node@3.1.3 preinstall script './scripts/download' 设置linux proxy (代理)的方式
    github命令行实用操作
    H5无障碍旁白模式使用网页支持
    Vue框架搭建入门到熟悉
    解决IOS下返回不刷新的问题
    小程序—跳转 数据传递
    微信小程序——地图
    常用的正则判断
    JS 控制输入框输入表情emoji 显示在页面上
  • 原文地址:https://www.cnblogs.com/bovine/p/2265302.html
Copyright © 2020-2023  润新知