如何在一个函数中运行Mersenne Twister? [英] How to run Mersenne Twister inside a function?
问题描述
我有一小段代码运行Mersenne Twister PRNG,工作原理很好:
std :: random_device randDev ;
std :: mt19937 twister(randDev());
std :: uniform_int_distribution< int> dist(0,99);
for(int i = 0; i <10; i ++){
std :: cout< dist(twister) std :: endl;
}
它输出十个随机数。但是,如果我把完全相同的代码放入一个函数:
#include< random&
int getRand(const int& A,const int& B){
std :: random_device randDev;
std :: mt19937 twister(randDev());
std :: uniform_int_distribution< int> dist(A,B);
return dist(twister);
}
int main(){
for(int i = 0; i< 10; i ++){
std :: cout< getRand(0,99)<< std :: endl;
}
return 0;
}
它输出相同的数字十次。我刚刚开始使用C ++,所以我不知道是什么原因或如何解决问题。
编辑:问题在于std :: random_device。它可能是在Eclipse C + + IDE(Luna版本)或MinGW 4.8.1中的错误,但无论什么原因,随机数总是相同的。
编辑2:考虑TC的建议和事实,我认为时间(0)将是我使用的一个合适的种子。<那个时间(0)仍然导致十个相同的数字,这里是最后的代码到目前为止。我知道rand()是坏的,但它工作。
#include< iostream>
#include< random>
std :: mt19937 twister(rand());
int getRand(const int& A,const int& B){
std :: uniform_int_distribution< int> dist(A,B);
return dist(twister);
}
int main(){
for(int i = 0; i <10; i ++){
std :: cout< getRand(0,99)<< std :: endl;
}
return 0;
}
使用?
Visual Studio 2012(不确定2013年)没有一个真正的实现std :: random_device,这意味着它被初始化相同的值每次 - 所以在这种情况下,你会有梅森扭曲器初始化相同
顺便说一句,似乎同样的问题在:
> 为什么我使用std :: random_device与mingw gcc4.8.1 获取相同的序列
所以显然GCC4.8在Windows上也有std :: random_device实现为伪-random。
I have a short piece of code which runs the Mersenne Twister PRNG and it works great:
std::random_device randDev;
std::mt19937 twister(randDev());
std::uniform_int_distribution<int> dist(0,99);
for (int i = 0; i < 10; i++) {
std::cout << dist(twister) << std::endl;
}
It outputs ten random numbers. However if I put the exact same code into a function:
#include <random>
int getRand(const int& A, const int& B) {
std::random_device randDev;
std::mt19937 twister(randDev());
std::uniform_int_distribution<int> dist(A,B);
return dist(twister);
}
int main() {
for (int i = 0; i < 10; i++) {
std::cout << getRand(0,99) << std::endl;
}
return 0;
}
It outputs the same number ten times. I'm just starting out with C++ so I have no idea what causes this or how to go about fixing the problem.
EDIT: The problem lies with std::random_device. It could be a bug in Eclipse C++ IDE (Luna version) or MinGW 4.8.1 but for whatever reason that random number is always the same. I believe time(0) will be a suitable seed for my uses.
EDIT 2: Taking T.C.'s suggestion into account and the fact that time(0) still results in ten of the same number, here's the final code so far. I know rand() is bad but it works.
#include <iostream>
#include <random>
std::mt19937 twister(rand());
int getRand(const int& A, const int& B) {
std::uniform_int_distribution<int> dist(A,B);
return dist(twister);
}
int main() {
for (int i = 0; i < 10; i++) {
std::cout << getRand(0,99) << std::endl;
}
return 0;
}
What compiler and OS do you use? Visual Studio 2012 (not sure about 2013) didn't have a real implementation of std::random_device, which means that it was initialized with same value each time - so in such case you would have mersenne twister initialized with same seed each time, thus same result.
And by the way, seems that same question was asked in:
why do I get same sequence for everyrun with std::random_device with mingw gcc4.8.1
so apparently GCC4.8 on Windows also has std::random_device implemented as pseudo-random.
这篇关于如何在一个函数中运行Mersenne Twister?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!