【发布时间】:2018-02-05 19:53:06
【问题描述】:
//My trial program
#include<iostream>
#include<random>
using namespace std;
int main(){
//USed to initialize (seed) the random number generator
random_device sd{};
// The random number generator
mt19937 engine {sd()};
//Uniformly distribute random numbers in [1...10]
uniform_int_distribution <> dis{1, 50};
//Generate a random integer
int x {dis(engine)};
//Print it
cout<<x<<"\n";
return 0;
}
我已经使用上面的代码生成了 1 到 50 之间的随机数。但是每当我运行程序时,生成的随机数都是一样的。我正在学习的一个在线课程有这个代码,它在讲师的 clang 编译器上运行得非常好。我正在使用 gcc 编译器。谁能告诉我需要做什么?谢谢!!
【问题讨论】:
-
不断更换种子
-
mt19937 引擎 {sd()};这里有任何兰特值
-
你有机会使用 MinGW 吗?
std::random_device已经或曾经遇到过一些问题。 -
"//在[1...10] uniform_int_distribution dis{1, 50};中均匀分布随机数;":为什么注释代码是错误的。跨度>
-
@HariomSingh 我所指的代码在 mt19937 引擎 {sd()} 上没有任何 rand 值;它仍在工作。
标签: c++ random numbers mt19937