C ++线程安全的均匀分布随机数生成 [英] C++ thread-safe uniform distribution random number generation
问题描述
我有一个循环. 在循环内部,在每次迭代中,我都需要从U [0,1]中提取一个数字. 如何使用openmp,并确保随机数生成过程未被污染?
I have a loop. Inside the loop, in each iteration, I need to draw a number from U[0,1]. How can I use openmp, and also make sure that the random number generating process is not contaminated?
我建议我需要一个线程安全的随机数生成器,它可以解决我的问题,也可以不是.
I got suggestion that I need a thread-safe random number generator, which may or may not be the solution to my problem.
我的问题与另一个问题非常相关,我想从conuinum U [0,1]中得出一点点差异.另外,我不知道如何按线程播种生成器,有人可以写一行代码吗?
My question is very related to another one, with a slight difference that I want to draw from a coninuum U[0,1]. Additionally, I don't know how to seed generator by thread, can someone please write a line of code?
推荐答案
Based on the already mentioned solution, here is a version adapted to your specific needs:
double doubleRand(double min, double max) {
thread_local std::mt19937 generator(std::random_device{}());
std::uniform_real_distribution<double> distribution(min, max);
return distribution(generator);
}
这篇关于C ++线程安全的均匀分布随机数生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!