【问题标题】:Finding Probability on a biased die在有偏的骰子上寻找概率
【发布时间】:2019-05-05 09:01:30
【问题描述】:

我必须模拟一个有偏差的骰子,以使 6 的出现次数超过 50%。我能够使用:

from random import randint,choice
def bdie():
    out = random.randint(0,2)
    if (out==1 or out == 2):
        return 6
    else:
        r = choice([i for i in range(1,7) if i not in [6]])
        return r

def bdthrow(n):
    output = []
    for i in range(0,n):
        if n>0 & n<=100000000:
            outcome = bdie()
            output.append(outcome)
        else:
            print("Invalid")
    print(output)

输出将是:

[6, 6, 6, 6, 6, 3, 6, 3, 5, 4]

现在在这个有偏差的骰子上,我应该找到顶面为 5 的概率,我需要找到骰子每个面的平均计数。

现在在纸上求解总和很容易,我可以在其中找到概率,但我不确定如何在 python 中实现它。

【问题讨论】:

  • 你需要滚动 5 的实现概率吗? (即滚动值是多少次 5 的计数)或在 P(6>0.5) 的情况下获得 5 的纯理论概率?
  • 我只需要不考虑投掷次数的纯概率。
  • 我不明白这个问题:你想知道如何在 python 中计算总和吗?还是阴谋?还是两者都有?
  • 我只是稍微编辑了这个问题。我只需要帮助计算得到 5 的概率并在这种有偏差的骰子上获得每张脸的平均计数。希望这可以清除它。

标签: python probability probability-theory


【解决方案1】:

如果我理解正确,您正在寻找获得 5 的无偏估计量。这样的估计量可以是掷骰子足够次数时获得 5 的数量。即# fives / n

从内存的角度来看,我建议使用defaultdict。此外,无需在每一轮中检查n 的值。

from random import randint,choice
from collections import defaultdict

def bdie():
    out = randint(0,2)
    if (out==1 or out == 2):
        return 6
    else:
        r = choice([i for i in range(1,7) if i not in [6]])
        return r

def bdthrow(n):
    output = defaultdict(int)
    for i in range(0,n):
        outcome = bdie()
        output[outcome] +=1
    return (float(output[5])/n)

对代码几乎没有其他优化,但天真地应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    • 2013-12-29
    • 2015-07-03
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多