CMD中的随机数生成器c++错误
Random Number Generator C++ Error in CMD
我正在尝试制作一个随机数生成器,生成一个介于0和999之间的数字。
我最初确实让它在mt19937的种子从time(null)生成的地方运行,但发现这会导致数字每秒更改一次,并且当我在for循环中再次调用它时速度不够快。
我使用code::块来编译我的代码,它编译没有错误,但是当我运行代码时,我在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中运行了完全相同的代码,并且没有错误。
std::random_device
实际上不需要实现。
第三段。如果设备不好,他们会抛出异常。尝试使用不同的种子RNG
你的函数从不返回值。因为你有一个int
返回类型,你的函数必须返回一个可以转换为int
的东西。如果您不想返回任何东西,可以将函数更改为void
。
因为你的异常引用了构造函数的名字,所以看起来随机设备不能被创建。c++ 14标准26.5.6.4有
抛出:一个实现定义类型的值,该值派生自random_device无法初始化。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 错误:未在此范围内声明'reverse'