确定掷骰子中数字出现的频率
对于游戏,我试图确定在给定的骰子数量下特定的 # 出现的频率。 我知道……这个问题看起来很奇怪。 让我尝试用实际数字来解释它。
因此,对于 1 个骰子,每个数字的频率将相同。 1-6 将出现相同的次数。
现在对于 2 个骰子,情况会变得不同。 我想 5、6、7 将是最常滚动的,而频谱两端的数字将出现较少或根本不出现(在 1 的情况下)。 我想知道如何计算此列表并按正确的顺序(从最频繁到不太频繁)显示它们。
有什么想法吗?
@duffymo - 如果有某种算法来提出它,那就太好了。 看来上述方法需要大量的手工挑选和放置数字。 如果我的骰子数量是动态的,例如 10,那么我认为手动执行此操作将是低效且麻烦的。 :)
For a game I'm trying to determine the frequency that a certain # will show up at a given # of dice being rolled. I know... that question seems odd. Let me try to explain it with real numbers.
So, for 1 die, the frequency for each number will be identical. 1-6 will show up equal number of times.
Now for 2 dice, things get different. I imagine 5,6,7 are going to be the most frequently rolled, while numbers at both ends of the spectrum will show up less or not at all (in the case of 1). I'd like to know how to calculate this list and show them in the proper order, from most frequent to less frequent.
Any thoughts?
@duffymo - It would be really nice though to have some sort of an algorithm to come up with it. It seems that the above way is going to require a lot of hand picking and placing of numbers. If my die count is dynamic up to say 10, doing that by hand will be ineffecient and troublesome I think. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
网上有很多关于骰子概率的内容。 这是一个帮助我解决欧拉项目问题的链接:
http://gwydir .demon.co.uk/jo/probability/calcdice.htm
There's lots of stuff online about dice probability. Here's one link that helped me out with a Project Euler question:
http://gwydir.demon.co.uk/jo/probability/calcdice.htm
纯粹的事实...
您知道帕斯卡三角形是 N 个 2 面骰子之和的概率分布吗?
Neat factoid...
Did you know that Pascal's triangle is the probability distribution of the sums of N 2-sided dice?
使用动态函数创建的 JavaScript 实现:
编辑: 没有人指出这一点,相当失望; 必须将
6 * dice
替换为Math.pow(6, dice)
。 以后不要再犯这样的错误了...JavaScript implementation using dynamic function creation:
EDIT: Rather disappointed nobody pointed this out; had to replace
6 * dice
withMath.pow(6, dice)
. No more mistakes like that...围绕到底“为什么”这似乎存在一些谜团,尽管达菲莫已经解释了其中的一部分,但我正在看另一篇文章,上面写着:
这有一定的吸引力。 但这是不正确的......因为第一次掷骰会影响机会。 通过示例可能最容易完成推理。
假设我想弄清楚两个骰子掷出 2 或 7 的概率是否更大。 如果我掷出第一个骰子并得到 3,那么我现在掷出总共 7 的机会有多大? 显然,六分之一。我总共掷出 2 的机会有多大? 6 中的 0...因为我无法在第二个骰子上滚动以使我的总数为 2。
因此,7 非常(最)有可能被滚动...因为无论我在第一个骰子上滚动什么骰子,我仍然可以通过在第二个骰子上滚动正确的数字来达到正确的总数。 6 和 8 的可能性同样稍小,5 和 9 的可能性更大,依此类推,直到我们达到 2 和 12,同样可能性为 36 分之一。
如果你绘制这个曲线(总和与可能性),你将得到一条漂亮的钟形曲线(或者更准确地说,由于实验的离散性质,得到一条近似的块状曲线)。
There seems to be some mystery surrounding exactly "why" this is, and although duffymo has explained part of it, I'm looking at another post that says:
There's a certain appeal to this. But it's incorrect...because the first roll affects the chances. The reasoning can probably most easily be done through an example.
Say I'm trying to figure out if the probability of rolling 2 or 7 is more likely on two dice. If I roll the first die and get a 3, what are my chances now of rolling a total of 7? Obviously, 1 in 6. What are my chances of rolling a total of 2? 0 in 6...because there's nothing I can roll on the second die to have my total be 2.
For this reason, 7 is very (the most) likely to be rolled...because no matter what I roll on the first die, I can still reach the correct total by rolling the right number on the second die. 6 and 8 are equally slightly less likely, 5 and 9 more so, and so on, until we reach 2 and 12, equally unlikely at 1 in 36 chance apiece.
If you plot this (sum vs likelyhood) you'll get a nice bell curve (or, more precisely, a blocky aproximation of one because of the discrete nature of your experiment).
经过在互联网和stackoverflow上进行大量搜索后,我发现 博士。 数学在工作函数中很好地解释了它(另一个答案中的链接有一个不正确的公式)。 我将 Dr. Math 的公式转换为 C#,并且我的 nUnit 测试(之前在代码的其他尝试中一直失败)全部通过。
首先,我必须编写一些辅助函数:
由于 select 在数学中的工作方式,我意识到如果我有一个具有下限的重载 Factorial 函数,我可以减少计算量。 当达到下限时,此功能可以退出。
当这些就位后,我就可以写了
After a lot of searching on the Internet and stackoverflow, I found Dr. Math explains it well in a working function (a link in another answer has an incorrect formula). I converted Dr. Math's formula to C# and my nUnit tests (which had been failing before with other attempts at the code) all passed.
First I had to write a few helper functions:
Because of the way choose works in mathematics, I realized I could cut down on the calculations if I had an overloaded Factorial function with a lower bound. This function can bail out when the lower bound is reached.
When those were in place, I was able to write
两个骰子有 6*6 = 36 种组合。
2=1+1只能出现一次,所以它的频率是1/36。
3 = 1+2或2+1,所以它的频率是2/36 = 1/18。
4 = 1+3、2+2 或 3+1,因此其频率为 3/36 = 1/12。
剩下的你可以做到十二点。
任何双陆棋玩家都清楚这些。
There are 6*6 = 36 combinations for two dice.
2 = 1+1 can only appear once, so its frequency is 1/36.
3 = 1+2 or 2+1, so its frequency is 2/36 = 1/18.
4 = 1+3, 2+2, or 3+1, so its frequency is 3/36 = 1/12.
You can do the rest out to twelve.
Any backgammon player knows these well.
递归方法的粗略草稿:
除非我弄错了,否则这应该会吐出像[键,频率]这样组织的KeyValuePairs。
编辑:仅供参考,运行此命令后,它显示 GetFrequenciesByOutcome(2, 6) 的频率为:
2: 1
3: 2
4: 3
5: 4
6: 5
7: 6
8: 5
9:4
10:3
11:2
12:1
Rough draft of a recursive way to do it:
Unless I'm mistaken, this should spit out KeyValuePairs organized like [key, frequency].
EDIT: FYI, after running this, it shows the frequencies for GetFrequenciesByOutcome(2, 6) to be:
2: 1
3: 2
4: 3
5: 4
6: 5
7: 6
8: 5
9: 4
10: 3
11: 2
12: 1
不需要真正的“算法”或模拟 - 这是基于 De Moivre 导出的公式的简单计算:
http://www.mathpages.com/home/kmath093.htm
而且它不是“钟形曲线”或正态分布。
There is no real "algorithm" or simulation necessary - it's a simple calculation based on a formula derived by De Moivre:
http://www.mathpages.com/home/kmath093.htm
And it's not a "bell curve" or normal distribution.
通过移动其位置,将之前滚动的频率数组“边数”倍相加,然后您将得到每个数字显示的频率数组。
这比暴力模拟要快得多,因为简单的方程是最好的。
这是我的 python3 实现。
例如,
请注意,您应该使用“目标数字 - 滚动计数”作为列表的索引来获取每个数字的频率。 如果您想获得概率,请使用“边数”^“滚动计数”作为分母。
这段代码产生
Add up the array of frequency of previous rolls, 'side number' times by shifting it's position, then you will get the array of frequencies each numbers show up.
This is much faster than brute force simulation, since simple equation is the best.
Here is my python3 implementation.
for example,
Note that, you should use 'target number - roll count' as index of the list to get frequency of each number. If you want to get probabilities, use 'side number'^'roll count' as a denominator.
This code yeilds