C++问题 - 有人可以帮忙吗?
你介意帮我解决以下问题吗?谢谢。
问题: 一次性掷出第 6 面 3 个骰子的百分比可以通过数学计算或 模拟。蒙特卡罗方法是一个计算机过程,用于找出问题的解决方案 通过计算机模拟问题。编写一个程序,掷三个骰子,计算它们的值 求和,并找出滚动每个可能结果的概率。
给定一个骨架程序 q1dsculpture.c,它生成滚动 a 的统计信息 六面骰子 10000 次。修改程序,使其生成掷出三个六面骰子的总和的统计数据。程序输出的示例在 下列的。请注意,由于骰子滚动的随机性。
提示:每次掷骰子时调用 rand() 三次,每个骰子调用一次。
骨架:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 7
int main() {
int face, roll, frequency[SIZE] = { 0 };
srand( time(NULL) );
for (roll = 1; roll <= 10000; roll++) {
face = rand() % 6 + 1;
++frequency[face];
}
printf("%s%12s\n", "Face", "Frequency");
for (face = 1; face <= SIZE - 1; face++)
printf("%4d%12d\n", face, frequency[face]);
getchar();
}
输出:
Face Frequency
3 49
4 129
5 276
6 481
7 669
8 994
9 1131
10 1213
11 1269
12 1197
13 962
14 707
15 464
16 268
17 144
18 47
Would you mind helping me to solving the following question? Thanks.
Question:
The percentage of rolling 3 dices of face 6 in one go can be found out mathematically or
simulation. The Monte Carlo method is a computer process to find out the solution of a
problem by computer simulation. Write a program that roll three dices, calculate their
sum, and find out the probability of rolling each possible outcome.
You are given a skeleton program q1dskeleton.c that generates the statistics of rolling a
dice of six sides 10000 times. Modify the program so that it generates the statistics of the sum of rolling three six-sided dices. An example of the program output is given in the
following. Note that because of the random nature of dice rolling.
Hint: Call rand() three times for every throw of the dices, one for each dice.
Skeleton:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 7
int main() {
int face, roll, frequency[SIZE] = { 0 };
srand( time(NULL) );
for (roll = 1; roll <= 10000; roll++) {
face = rand() % 6 + 1;
++frequency[face];
}
printf("%s%12s\n", "Face", "Frequency");
for (face = 1; face <= SIZE - 1; face++)
printf("%4d%12d\n", face, frequency[face]);
getchar();
}
output:
Face Frequency
3 49
4 129
5 276
6 481
7 669
8 994
9 1131
10 1213
11 1269
12 1197
13 962
14 707
15 464
16 268
17 144
18 47
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
显然,您必须增加
SIZE
因为不再有 6 种可能性,而是 18 种 - 我将在此处包括 1 和 2 的不可能总数,因为您的原始解决方案包括不可能的值 0 :-)然后,您不再扔一个骰子,而是扔三个,然后将骰子的值相加。所以类似于(伪代码):
加法就是您用来增加特定数组元素的内容。
为了获得奖励积分,您应该避免打印出不可能的结果。这是对最终
for
语句起始位的相当简单的修改。顺便说一下,您期望的比率是
{1, 3, 6, 10, 15, 21, 25, 27, 27, 25, 21, 15, 10, 6, 3, 1}
因此“理想”输出应该相当接近:(尽管这只加起来只有 9992 个样本 - 至于其他 8 个样本出现在哪里,这是一个冒险)。
Obviously, you have to increase
SIZE
since there are no longer 6 possibilities, but 18 - I'll include the impossible totals of 1 and 2 here since your original solution included the impossible value of zero :-)Then, instead of throwing one dice, you throw three and then add up the values. So something like (pseudo-code):
That addition is what you then use to increase a specific array element.
For bonus points, you should probably avoid printing out the impossible outcomes. That's a fairly simple modification to the starting bit of your final
for
statement.The ratios you expect, by the way, are
{1, 3, 6, 10, 15, 21, 25, 27, 27, 25, 21, 15, 10, 6, 3, 1}
so the "ideal" output should be reasonably close to:(even though that only adds up to 9992 samples - it's a crapshoot as to where the other eight show up).
另外,在计算 rand() 时,您需要意识到它并不像我们希望的那样随机。
来自文档: http://www.cplusplus.com/reference/clibrary/cstdlib /兰特/
“请注意,虽然这种模运算不会在跨度内生成真正均匀分布的随机数(因为在大多数情况下,较小的数字更有可能出现),但对于短跨度来说,它通常是一个很好的近似值。”
因此,为了获得更好的结果,最好做一些工作,为随机结果概率更高的兰特范围获取不同的公式。
例如,对于骰子,您会得到一个 30 到 90 之间的随机数,公式为 (int)(((float)result - 30) / 10) + 0.5 或类似的值。
然后,对于骰子 2,您可以获得 500 到 50000 之间的数字并制定另一个公式。
为了让你的作业更有趣,你可以看看不同的在线赌场/扑克室是如何做到这一点的。
http://www.fulltiltpoker.com/random-number-generator
Also, when calculating rand() you need to realize it is not all that random as we hope.
From the documentation at: http://www.cplusplus.com/reference/clibrary/cstdlib/rand/
"Notice though that this modulo operation does not generate a truly uniformly distributed random number in the span (since in most cases lower numbers are slightly more likely), but it is generally a good approximation for short spans."
So to get better results it is best to do some work on getting a different formula for a rand range that has a higher probability of random results.
For instance, for dice one you get a random number between 30 and 90 and the formula is (int)(((float)result - 30) / 10) + 0.5 or something along those lines.
Then for dice 2 you could get a number between 500 and 50000 and make another formula.
To make your homework assignment more interesting you could have a look at how different online casinos/poker rooms do this.
http://www.fulltiltpoker.com/random-number-generator