I'm trying to build a thread-safe random number generator, without any luck. I looked for some solutions, but none of them worked for some reason, can you help me figure out why and show me how to do it?. Here is the struct:
struct A{
std::thread tid;
std::default_random_engine generator;
float val_0;
float val_1;
void start(float lb, float ub){
tid = std::thread([&lb, &ub, this](){
generator.seed(std::random_device{}());
val_0 = R_0(generator, lb, ub);
val_1 = R_1(lb, ub);
});
}
void wait(){
tid.join();
}
float R_0(std::default_random_engine& generator, float lower_bound=0.0, float upper_bound=1.0){
return std::uniform_real_distribution<float>{lower_bound, upper_bound}(generator);
}
float R_1(float lower_bound=0.0, float upper_bound=1.0){
static thread_local std::mt19937 generator(std::random_device{}());
return std::uniform_real_distribution<float>{lower_bound, upper_bound}(generator);
}
};
As you can see, I tried two alternatives (R_0, R_1) but both provides garbage values. Here is an output example of 10 start() invokes:
0.502351
-8.97876e-14
0.93787
0.203829
0.0451958
0.118904
0.783515
0.155594
0.649292
-4.22577e-13
Aucun commentaire:
Enregistrer un commentaire