我想使用从 1 到 6 的随机数生成器模拟掷两个骰子,然后将这两个值加在一起,我需要将其设置为循环执行一百万次。获取值后,我需要显示每个数字出现的总和数。我该怎么做?我在正确的轨道上吗?我一直在转储我的核心,所以我认为这是一个内存问题。
这是我应该遵循的文本: “假设你有一个游戏,你掷两个骰子,遵循以下规则: throw 1-11:获得该值 第 12 卷:重新掷骰并将值加一 “roll 12”规则可以多次命中。例如,如果你掷 (6,6) 然后 (6,6) 然后 (1,1), 该游戏的总值(value)为 4(即 1+1+2=4)。编写一个程序来模拟这个游戏的一次迭代。 然后编写一个循环运行该游戏 1,000,000 次,跟踪您看到的每个值的数量,然后显示显示每个值的结果以及您获得该值的时间百分比。
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <algorithm>
using namespace std;
int main ()
{
int i, k, l, y, ncount;
k=0;
l=0;
y=0;
int R[1000000];
int T[1000000];
int S[1000000];
srand(time(NULL));
for (i=0; i<1000000; i++)
{
R[k] = (rand() % 6 + 1);
T[l] = (rand() % 6 + 1);
S[y] = R[k] + T[l];
k++;
l++;
y++;
}
ncount = count (S, S+1000000, 1);
cout << "1 appears " << ncount << " times.\n";
return 0;
}
最佳答案
我喜欢这个 - 非常可爱 - 实验。
这是一段您不能使用的代码示例(它使用了您的类(class)绝对不允许的各种库内容和 C++14 优点,从统计学上讲,如果您转过它会引起很多人的注意中)。
然而,它可以作为灵感并验证您的无偏差随机数生成和正确的统计数据!
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics.hpp>
#include <iomanip>
#include <iostream>
#include <random>
namespace ba = boost::accumulators;
namespace bat = ba::tag;
using namespace std;
static mt19937 engine { random_device{}() };
static uniform_int_distribution<unsigned> dist(1, 6);
auto single() {
return dist(engine);
}
auto dual() {
auto a = single(), b = single();
//cout << "Rolled (" << a << ", " << b << ")\n";
return a + b;
}
auto magic_roll() {
int eyes, extra = 0;
while (12 == (eyes=dual()))
extra += 1;
return extra + eyes;
}
int main() {
ba::accumulator_set<unsigned, ba::stats<bat::mean, bat::variance> > stats;
constexpr auto N = 15;
size_t histo[N] = { 0 }; // allow for quite extreme outliers
for (size_t i = 0; i < 1'000'000; ++i) {
auto score = magic_roll();
//cout << "score: " << score << "\n";
stats(score);
assert(score >= 2 && score < N);
++histo[score];
}
cout << "Mean: " << ba::mean(stats) << "\n";
cout << "Std Deviation: " << sqrt(ba::variance(stats)) << "\n";
auto peak = *max_element(begin(histo), end(histo));
auto scale = [peak](auto v) { return v * 60.0 / peak; };
auto bucket = 0;
for(auto v : histo)
{
cout << "Histo bucket: " << right
<< setw(2) << bucket++ << "\t"
<< setw(6) << v << " " << string(scale(v), '*') << "\n";
}
}
输出
Mean: 6.88604
Std Deviation: 2.29999
Histo bucket: 0 0
Histo bucket: 1 0
Histo bucket: 2 27806 *********
Histo bucket: 3 56229 *******************
Histo bucket: 4 84624 *****************************
Histo bucket: 5 113481 ***************************************
Histo bucket: 6 142361 **************************************************
Histo bucket: 7 170696 ************************************************************
Histo bucket: 8 143744 **************************************************
Histo bucket: 9 114814 ****************************************
Histo bucket: 10 86860 ******************************
Histo bucket: 11 57734 ********************
Histo bucket: 12 1611
Histo bucket: 13 39
Histo bucket: 14 1
关于c++ - 如何将随机数组值一起添加到另一个数组中,然后在计算出现的值的同时显示它们? C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33468616/