Chen

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.

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?

MikeMB

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);
}
```

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at

*1*### Correct OpenMP pragmas for pi monte carlo in C with not thread-safe random number generator

*2*### How do I generate thread-safe uniform random numbers?

*3*### C - random number generation

*4*### D std.random different behavior between integer and decimal uniform random number generation

*5*### Pseudo-random number generation in MEX C

*6*### Understanding uniform random number generation

*7*### Very fast uniform distribution random number generator

*8*### Random Uniform Distribution

*9*### Generating uniform distribution using Math.random()

*10*### Random non-uniform distribution with given proportion

*11*### On the random number generation dSFMT performance on C++

*12*### Finding the distribution through random number generation in R

*13*### Generate random numbers with uniform distribution (getting same number in loop)

*14*### C++ Are pseudo random number generators thread safe?

*15*### Efficient random number generation with C++11 <random>

*16*### Spark - Random Number Generation

*17*### rand() Random Number Generation C++

*18*### Correct OpenMP pragmas for pi monte carlo in C with not thread-safe random number generator

*19*### How do I generate thread-safe uniform random numbers?

*20*### Random vector a with elements from the uniform distribution in R

*21*### D std.random different behavior between integer and decimal uniform random number generation

*22*### Random Uniform Distribution

*23*### Finding the distribution through random number generation in R

*24*### Generate random numbers with uniform distribution (getting same number in loop)

*25*### does random number in the real world follow uniform distribution

*26*### Making own random number class compitible with uniform_int_distribution

*27*### Generating a random integer with non-uniform distribution

*28*### GSL+OMP: Thread safe random number generators in C++

*29*### Random real numbers with uniform distribution in verilog

## Comments