C++2骰子滚动1000万次开始
C++ 2 dice rolling 10 million times BEGINNER
我正在尝试创建一个程序,该程序将掷2个骰子1000万次,并输出每个数字的掷次数。除此之外,我的任务是为输出创建一个直方图(*=2000(。这是我迄今为止所拥有的。
/*
Creating a program that counts outcomes of two dice rolls, then show a
histogram of the outcomes.
Section 1 : Simulate ten million times rolls of two dice, while counting
outcomes. (Hint: Use an array of size 13.)
Section 2 : Show the outcome, the numbers of outcomes, and the histogram
(one * designates 20000). Your output must align properly.
*/
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int main()
{
int i, j, ary[13] = {};
cout << "Please enter the random number seed.";
cin >> j;
srand(j);
for (i = 0; i < 10000000; i++)
ary[die() + die()]++;
for (i = 2; i <= 12; i++)
{
cout << setw(3) << i << " : " << setw(6) << ary[i] << " : ";
for (j = 0; j < ary[i]; j += 2000)
cout << "*";
cout << endl;
}
return 0;
}
示例输出:https://i.stack.imgur.com/duxQz.jpg
我知道我需要用rand((%6+1;在节目的开头。我觉得我已经接近完成了,但缺少了关键点!我也意识到我没有在我的ary[]中定义die((
我建议使用高精度计时器创建随机种子,例如std::chrono::high_resolution_clock。然后它们不依赖于用户,实际上是随机的。总是在调用std::rand之前创建种子。
#include <chrono>
auto time = std::chrono::high_resolution_clock::now();
auto seed = std::chrono::duration_cast<std::chrono::milliseconds>(time);
std::srand(seed)
毫秒精度使种子通常足够独特,但如果种子每秒需要接近1000次,那么我建议使用纳秒或微秒精度来实现真正的随机性。
最好是创建一个函数,使用高精度计时器和随机值创建随机种子,并最终确保返回值在0和5之间(对于6面骰子(。
相关文章:
- 当回溯以零开始时,如何调试崩溃
- 根据用户输入用字母填充矢量,并将"开始"和"结束"放在四肢
- 有哪些有效的方法可以消除一组 100 万个字符串>重复数据?
- 如何从头开始创建 avi 文件?
- 为什么 -mmacosx-version-min=10.10 不阻止使用标记为从 10.11 开始的函数?
- 自一周开始以来的秒数?
- 一旦双簧管录制开始,主 UI 线程就会被阻止
- GStreamer在开始任何播放之前进行搜索
- 使 std::fstream 写入文件末尾,但从头开始读取
- 如何以毫秒为单位获取开始时间和 now() 之间的毫秒差异(以 C++为单位?
- 哪个C++规范开始支持 std::vector
- 如何继续读取不同功能中的文件,而不是从头开始再次读取文件?
- 如何在 c++ 中确定一条指令(以字节为单位)在哪里结束,另一条指令从哪里开始?
- 如何在调试中通过传递引用开始
- 如何使用 picojson.h 在 c++ 中从头开始创建 json 对象
- 从特定键开始遍历地图
- C++2骰子滚动1000万次开始
- 这是否可以通过 C++ 或 C# 中的某种数学运算将 1000 万位数字转换为 1000 位数字
- 128 位 MD5 在不同数量的文件(例如 1000 万或 5000 万)上的冲突率是多少?
- 不能deque.push_back()1000万+德克