python投骰子程序代码_用于双骰子(一个偏向一个法线)仿真的Python程序

python投骰子程序代码

Here, we will be simulating the occurrence of the sum of the faces of two dice [i.e. dice(A) - 1, 2, 3, 4, 5 ,6 + dice(B) - 1, 2, 3, 4, 4, 4, 5, 6, 6 ,6]. A dice is normal(each has an equal probability of occurrence) and another B dice is biased one (each face has not an equal probability of outcome). So, in this case, we have to find out the maximum probable sum if the dice. So we simply take the help of stimulation to do so. Simply we are going to use an inbuilt library called as random to call a random value from given set and thereby we can stimulate the occurrence value by storing the occurrence in the list ls of length 12 representing each face of the dice as ls[4] represents the occurrence of face 5.

在这里,我们将模拟两个骰子的面之和发生 [即dice(A)-1,2,3,4,5,6 + dice(B)-1,2,3,4,4 ,4,5,6,6,6]。 一个骰子是正常的(每个骰子的发生概率均等),另一个B骰子的骰子是一个偏斜的骰子(每个面Kong的结局概率均不相同)。 因此,在这种情况下,我们必须找出最大骰子的和。 因此,我们只是借助刺激来做到这一点。 简单地,我们将使用一个称为random的内置库从给定集合中调用一个随机值,从而可以通过将出现次数存储在代表骰子每个面的长度为12的列表ls中作为ls [4]来刺激出现值。表示出现面5。

    ls[0]   - dice(1)
    ls[1]   - dice(2)
    ls[2]   - dice(3)
    ls[3]   - dice(4)
    ls[3]   - dice(5)
    ls[5]   - dice(6) 
    ls[6]   - dice(7)
    ls[7]   - dice(8)
    ls[8]   - dice(9)
    ls[9]   - dice(10)
    ls[10]  - dice(11)
    ls[11]  - dice(12) 

Then using the library pylab, we can plot the value of each occurrence and can stimulate it.

然后,使用库pylab,我们可以绘制每个事件的值并进行刺激。

The deviation is clear that each of the faces has an equal almost equal probability of occurrence.

显然,每个面都有相等的几乎相等的出现概率。

Program:

程序:

import random
import pylab as py

def roll():
    return random.choice([1,2,3,4,5,6])

def biased():
    return random.choice([1,2,3,4,4,4,5,5,5,6,6,6])

ls = [0,0,0,0,0,0,0,0,0,0,0,0]
chance = [104, 203, 302, 401, 505, 646, 756, 855, 985]
for n in chance:
    for k in range(n):
        scr = roll() + biased() 
        ls[scr-1] = ls[scr-1] + 4/4


py.figure()
py.plot([1,2,3,4,5,6,7,8,9,10,11,12], ls)

for el in ls:
    print(el)

Output

输出量

Double dice simulation

翻译自: https://www.includehelp.com/python/program-for-double-dice-one-biased-one-normal-simulation.aspx

python投骰子程序代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值