概率练习返回与预期不同的结果

发布于 2024-08-05 13:28:41 字数 875 浏览 1 评论 0原文

作为练习,我正在编写一个程序来计算掷出 5 个相同数字的骰子的几率。这个想法是通过模拟而不是简单的数学来获得结果。我的程序是这样的:

# rollFive.py

from random import *

def main():
    n = input("Please enter the number of sims to run: ")
    hits = simNRolls(n)
    hits = float(hits)
    n = float(n)
    prob = hits/n
    print "The odds of rolling 5 of the same number are", prob

def simNRolls(n):
    hits = 0
    for i in range(n):
        hits = hits + diceRoll()
    return hits


def diceRoll():
    firstDie = randrange(1,7,1)
    for i in range(4):
        nextDie = randrange(1,7,1)
        if nextDie!=firstDie:
            success = 0
            break
        else:
            success = 1
    return success

问题是,以 1 000 000 的 n 值运行这个程序给我的概率通常在 0.0006 和 0.0008 之间,而我的数学让我相信我应该得到一个接近 0.0001286 的答案(又名(1/ 6)^5)。

我的程序有问题吗?或者我在这里犯了一些基本的数学错误?或者,如果我能够在更大的迭代中运行程序,我会发现我的结果更接近正确的答案吗?

As an exercise I'm writing a program to calculate the odds of rolling 5 die with the same number. The idea is to get the result via simulation as opposed to simple math though. My program is this:

# rollFive.py

from random import *

def main():
    n = input("Please enter the number of sims to run: ")
    hits = simNRolls(n)
    hits = float(hits)
    n = float(n)
    prob = hits/n
    print "The odds of rolling 5 of the same number are", prob

def simNRolls(n):
    hits = 0
    for i in range(n):
        hits = hits + diceRoll()
    return hits


def diceRoll():
    firstDie = randrange(1,7,1)
    for i in range(4):
        nextDie = randrange(1,7,1)
        if nextDie!=firstDie:
            success = 0
            break
        else:
            success = 1
    return success

The problem is that running this program with a value for n of 1 000 000 gives me a probability usually between 0.0006 and 0.0008 while my math makes me believe I should be getting an answer closer to 0.0001286 (aka (1/6)^5).

Is there something wrong with my program? Or am I making some basic mistake with the math here? Or would I find my result revert closer to the right answer if I were able to run the program over larger iterations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

茶底世界 2024-08-12 13:28:41

获得特定数字五次的概率是 (1/6)^5,但获得任意五个相同数字的概率是 (1/6)^4。

有两种方法可以看到这一点。

首先,例如,得到全 1 的概率是 (1/6)^5,因为六种方法中只有一种得到 1。将其乘以 5 个骰子,就得到 (1/6)^5 。但是,由于有 6 个可能的数字可以获得相同的结果,因此有 6 种成功的方法,即 6((1/6)^5) 或 (1/6)^4。

从另一个角度来看,第一卷给出的结果并不重要,因此我们将其排除。然后我们必须将该数字与剩余的 4 次掷骰相匹配,其概率为 (1/6)^4。

The probability of getting a particular number five times is (1/6)^5, but the probability of getting any five numbers the same is (1/6)^4.

There are two ways to see this.

First, the probability of getting all 1's, for example, is (1/6)^5 since there is only one way out of six to get a 1. Multiply that by five dice, and you get (1/6)^5. But, since there are six possible numbers to get the same, then there are six ways to succeed, which is 6((1/6)^5) or (1/6)^4.

Looked at another way, it doesn't matter what the first roll gives, so we exclude it. Then we have to match that number with the four remaining rolls, the probability of which is (1/6)^4.

探春 2024-08-12 13:28:41

你的数学是错误的。获得五个相同数字的骰子的概率为 6*(1/6)^5 = 0.0007716

Your math is wrong. The probability of getting five dice with the same number is 6*(1/6)^5 = 0.0007716.

暮年 2024-08-12 13:28:41

很简单,掷 5 个骰子有 6 ** 5 种可能的结果,而其中只有 6 种是成功的,所以答案是 6.0 / 6 ** 5

Very simply, there are 6 ** 5 possible outcomes from rolling 5 dice, and only 6 of those outcomes are successful, so the answer is 6.0 / 6 ** 5

难忘№最初的完美 2024-08-12 13:28:41

正如您所说的那样,我认为您的预期概率是错误的。 (1/6)^5 是连续滚动某个特定数字 5 次的概率; (1/6)^4 是连续滚动任意数字 5 次的概率(因为第一次滚动总是“成功”——也就是说,第一次滚动总是会产生某个数字) )。

>>> (1.0/6.0)**4
0.00077160493827160479

与运行 100 万次迭代的程序相比:

[me@host:~] python roll5.py 
Please enter the number of sims to run: 1000000
The odds of rolling 5 of the same number are 0.000755

I think your expected probability is wrong, as you've stated the problem. (1/6)^5 is the probability of rolling some specific number 5 times in a row; (1/6)^4 is the probability of rolling any number 5 times in a row (because the first roll is always "successful" -- that is, the first roll will always result in some number).

>>> (1.0/6.0)**4
0.00077160493827160479

Compare to running your program with 1 million iterations:

[me@host:~] python roll5.py 
Please enter the number of sims to run: 1000000
The odds of rolling 5 of the same number are 0.000755
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文