I am fresh in C++, I met a problem about random number generation. I am actually doing simulation, for generating a Random Number, I create a function in a separated file: rng.h for example.
#include <random>//C++11
double rng(void){//uniform random generator. using MT19937;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<double> dis(0,1);
return dis(gen);
}
I called this function like this:
#include <iostream>
...
#include "rng.h"
int main(){
for(int i=0;i<10000;i++){
std::cout<<rng()<<std::endl;
}
func01();
func02();
func03();
.....
func99();
return 0;
}
In each funcXX(), rng() will be called as well. My code was compiled by g++ -std=c++11;
My Question is: after running for a while, CPU usage reach to 100%, and program was jamed.
I used gdb trace the problem, it showed:
^C
Program received signal SIGINT, Interrupt.
std::__detail::__mod<unsigned long, 4294967296ul, 1ul, 0ul> (__x=5223916446078556027)
at /usr/include/c++/4.8/bits/random.h:150
150 { return _Mod<_Tp, __m, __a, __c>::__calc(__x); }
or
^C
Program received signal SIGINT, Interrupt.
0x00000000004204f8 in std::__detail::_Mod<unsigned long, 624ul, 1ul, 0ul, true, true>::__calc (__x=319)
at /usr/include/c++/4.8/bits/random.h:142
142 __res %= __m;
or
^C
Program received signal SIGINT, Interrupt.
0x000000000040aae5 in std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>::seed (this=0x7fffffffb5d0, __sd=2650715069)
at /usr/include/c++/4.8/bits/random.tcc:338
338 __x *= __f;
......
Issues seemed unstable....
More interesting is sometimes it went through.
How can I fix this, many thanks.....
Aucun commentaire:
Enregistrer un commentaire