如何在python中模拟偏向死角? [英] How do I simulate biased die in python?

查看:87
本文介绍了如何在python中模拟偏向死角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要模拟N侧偏置芯片吗?

I want to simulate N-sided biased die?

def roll(N,bias):
     '''this function rolls N dimensional die with biasing provided'''
     # do something
     return result

>> N=6
>> bias=( 0.20,0.20,0.15,0.15,0.14,0.16,)
>> roll(N,bias)
   2

推荐答案

一点数学.

常规骰子将以相等的概率给每个数字1-6,即1/6.这称为均匀分布(离散版本,相对于连续版本).这意味着,如果X是描述单个角色结果的随机变量,则X~U[1,6]-意味着X与模辊的所有可能结果(从1到6)均等分布.

A regular die will give each number 1-6 with equal probability, namely 1/6. This is referred to as uniform distribution (the discrete version of it, as opposed to the continuous version). Meaning that if X is a random variable describing the result of a single role then X~U[1,6] - meaning X is distributed equally against all possible results of the die roll, 1 through 6.

这等同于在[0,1)中选择一个数字并将其分为6个部分:[0,1/6)[1/6,2/6)[2/6,3/6)[3/6,4/6)[4/6,5/6)[5/6,1).

This is equal to choosing a number in [0,1) while dividing it into 6 sections: [0,1/6), [1/6,2/6), [2/6,3/6), [3/6,4/6), [4/6,5/6), [5/6,1).

您正在请求其他分配,这是有偏见的. 实现此目的最简单的方法是根据所需的偏差将[0,1)部分分为6个部分.因此,在您的情况下,您希望将其分为以下几类: [0,0.2)[0.2,0.4)[0.4,0.55)0.55,0.7)[0.7,0.84)[0.84,1).

You are requesting a different distribution, which is biased. The easiest way to achieve this is to divide the section [0,1) to 6 parts depending on the bias you want. So in your case you would want to divide it into the following: [0,0.2), [0.2,0.4), [0.4,0.55), 0.55,0.7), [0.7,0.84), [0.84,1).

如果您查看维基百科条目,您会看到在这种情况下,累积概率函数将不由6个等长的部分组成,而是由6个长度不同的部分组成,这些部分根据您给它们的 bias 的不同而有所不同.质量分布也一样.

If you take a look at the wikipedia entry, you will see that in this case, the cumulative probability function will not be composed of 6 equal-length parts but rather of 6 parts which differ in length according to the bias you gave them. Same goes for the mass distribution.

回到问题,根据您使用的语言,将其翻译回您的模版纸.在Python中,这是一个非常粗略的示例,尽管可以正常工作:

Back to the question, depending on the language you are using, translate this back to your die roll. In Python, here is a very sketchy, albeit working, example:

import random
sampleMassDist = (0.2, 0.1, 0.15, 0.15, 0.25, 0.15)
# assume sum of bias is 1
def roll(massDist):
    randRoll = random.random() # in [0,1]
    sum = 0
    result = 1
    for mass in massDist:
        sum += mass
        if randRoll < sum:
            return result
        result+=1

print(roll(sampleMassDist))

这篇关于如何在python中模拟偏向死角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆