简体   繁体   English

使用蒙特卡罗方法在Python 3.2中查找特定骰子输出的特定概率

[英]Using Monte Carlo method to find a specific probability of a certain dice output in Python 3.2

I'm in a statistics class and we are constantly being given "dice problems" with various constrains. 我正处于统计课程中,我们不断受到各种约束的“骰子问题”。 This is a probability problem, where I need to evaluate the probability of an event by using the Monte Carlo method. 这是一个概率问题,我需要使用蒙特卡罗方法来评估事件的概率。 I know I could integrate my way through it but I'd like to write a programme that allows me to simply modify constraints in terms of how many dice I have, how many rolls I made and how many sides do those dice have. 我知道我可以通过它整合我的方式但是我想编写一个程序,允许我简单地修改约束,包括我有多少骰子,我制作了多少卷以及这些骰子有多少方面。

This is one of the problems I'm working on. 这是我正在研究的问题之一。 Simpler, cause I don't want to get carried away with my code writing "abilities". 更简单,因为我不想忘记我的代码写“能力”。

Assume that a dice have 9 sides each. 假设骰子各有9个边。 Estimate the probability that when you roll 5 dice at least 3 will have the same value. 估计滚动5个骰子时至少3个骰子具有相同值的概率。

This is the general template for the problems we are given: Roll an X number of n-‐sided dice, each of which has sides numbered from 1 to n. 这是我们给出的问题的一般模板:滚动X个n面骰子,每个骰子的边数从1到n。 Estimate the probability that we will get 3 or more dice with the same outcome. 估计我们获得3个或更多骰子的概率相同。

I want to write a function for the sample problem, lets say, which takes as input an integer n, which is the number of faces in each die, and computes the probability that 3 or more dice have the same value. 我想为样本问题编写一个函数,比方说,它将整数n作为输入,这是每个骰子中的面数,并计算3个或更多骰子具有相同值的概率。 My biggest problem is the constraint "at least 3 out of 5". 我最大的问题是约束“至少3/5”。 I've looked through other similar problems on Stackoverflow, but none of them really touch base with me. 我已经查看了Stackoverflow上的其他类似问题,但它们都没有真正触及我的基础。 How would you write the code for constraint? 你会如何编写约束代码? I am using Python 3.2. 我使用的是Python 3.2。

class Die(object):
  def __init__(self, sides = 9):
    self.sides = sides

  def roll(self):
    return randint(1, self.sides)

I'm stuck here. 我被困在这里 Any input is helpful, thanks! 任何输入都有帮助,谢谢!

I don't think I would use a class here. 我不认为我会在这里上课。 you just generate the dice roll and then check to see if the dice roll should be counted or not. 你只需要生成骰子卷,然后检查是否应该计算骰子卷。 In this case, I'd use a Counter to do the counting just to make the code a little more clean: 在这种情况下,我会使用Counter进行计数,以使代码更加干净:

from collections import Counter
from random import randint

def roll(ndice,nsides=9):
    return [randint(1,nsides) for _ in range(ndice)]

def count_it():
    c = Counter(roll(5))
    return c.most_common(1)[0][1] >= 3

ntries = 100000
print (sum(1 for _ in range(ntries) if count_it())/ntries)

It looks to me like you have about a 10% chance. 在我看来,你有大约10%的几率。 The trick with Monte-Carlo is determining whether you've converged or not. Monte-Carlo的诀窍在于确定你是否融合。 You can do this a few times with different numbers of ntries . 您可以使用不同数量的ntries执行此操作几次。 The bigger your make ntries , the smaller the spread will be in your output. 你的化妆越大ntries ,较小的价差将在您的输出。 Eventually, when the spread is small enough, you say you've converged on the solution with some certainty. 最终,当差价足够小时,你说你已经确定地收敛了解决方案。

Just use your Die class a large number of times: 只需使用你的Die类很多次:

# roll a lot of dice!
myDie = Die(9) # 9 sides

roll_counts = {side:0 for side in range(1, myDie.sides + 1)} 

numRolls = int(1e6)
for x in xrange(numRolls):
    roll_counts[myDie.roll()] += 1

Then analyze your distribution as needed: 然后根据需要分析您的分布:

for side in sorted(roll_counts):
    side_pct = float(roll_counts[side]) / numRolls * 100
    print 'side {} comprised {}% of all rolls'.format(side, side_pct)

EDIT: I'm aware this solution doesn't solve your homework problem, but hopefully it gives you the tools you need to roll and count your dice. 编辑:我知道这个解决方案并不能解决您的作业问题,但希望它能为您提供掷骰子和计数骰子所需的工具。 It's likely you'll need to do the above for multiple die at a time and have a way of comparing their equality at each roll. 您可能需要一次为多个模具执行上述操作,并且可以比较每个模具的相等性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM