Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread-safe uniform random number generator

I have some parallel Fortran90 code in which each thread needs to generate the same sequence of random numbers.

I have a random number generator that seems to be thread-unsafe, since, for a given seed, I'm completely unable to repeat the same results each time I run the program.

I surfed unsuccessfully (almost) the entire web looking for some code of a thread-safe RNG. Could anyone provide me with (the link to) the code of one?

Thanks in advance!

like image 223
Bellman Avatar asked Mar 02 '09 13:03

Bellman


People also ask

Is random number generator thread safe?

Both the functions on the “random” module are thread safe as are the methods on an instance of the random. Random class. This means that a multithreaded program may call module functions in order to generate random numbers with a seed and sequence of random numbers shared between threads, or have a single new random.

Is random class thread safe?

Instances of Random are thread–safe. Although, if the same instances are used across threads they may suffer from contention and result in poor performance. The Instances of Random are not cryptographically safe and therefore should not be used for security-sensitive applications.

Does Google have a random number generator?

The RAND function in Google Sheets generates a random number between 0 and 1 and can be used as a random number generator in Google Sheets. The RAND function outputs a decimal number between 0 and 1, for example, 0.2760773217.

Why is random not thread safe?

This is due to an implementation detail of Random that does not tolerate multithreaded access (it wasn't designed for it, and the docs explicitly call out that Random should not be used from multiple threads, as they do for most types in the . NET Framework).


2 Answers

A good Pseudorandom number generator for Fortran90 can be found in the Intel Math Kernel Vector Statistical Library. They are thread safe. Also, why does it need to be threadsafe? If you want each thread to get the same list, instantiate a new PRNG for each thread with the same seed.

like image 95
John Ellinwood Avatar answered Oct 21 '22 08:10

John Ellinwood


Most repeatable random number generators need state in some form. Without state, they can't do what comes next. In order to be thread safe, you need a way to hold onto the state yourself (ie, it can't be global).

like image 42
plinth Avatar answered Oct 21 '22 08:10

plinth