【发布时间】:2015-08-29 02:26:59
【问题描述】:
我正在尝试制作一个随机数生成器,以生成一个介于 0 和 999 之间的数字。
我最初确实在从 time(null) 生成 mt19937 的种子的地方运行它,但发现这会导致数字每秒更改一次,并且当我从内部再次调用它时速度不够快for 循环。
我正在使用 code::blocks 来编译我的代码,它编译时没有错误,但是当我运行代码时,我在 cmd 中出现错误。
Error: terminate called after throwing an instance of 'std::runtime_error'
what(): random_device::random_device(const std::string&)
我是不是做错了什么?
#include <iostream>
#include <random>
using namespace std;
//Random Number Generator
int numGen() {
//Random Number Generator
random_device rd;
mt19937 mt(rd());
uniform_int_distribution<int> dist(0, 999);
for (int I = 0; I < 6; i++) {
cout <<dist(mt) << " ";
}
cout <<endl;
}
更新:
我现在从 Visual Studio 运行完全相同的代码,没有错误。
【问题讨论】:
-
我在一个为你编译和执行的网站上检查了这段代码,运行没有问题
-
code::blocks is compatible with c++ 11 below option is enabled: "Have g++ follow the C++11 ISO C++ langage standard [-std=c++11]"
-
为什么每次都重新播种?
-
这样它就改变了从 for 循环中每次通过时生成的“dist”的值。