CHAPTER 3: Random Number Generators for PDC¶
Note that all examples in this chapter use a C++ library and are therefore C++ code examples.
Random number generators are algorithms that produce the next value in a stream of numbers for each repeated call to a function. These generators have been implemented in libraries that are built into a compiled language or have been developed separately. Traditional random number generators available in most compilers do not work correctly in threaded or distributed code.
In C++ threaded code there is no guarantee of thread safety when all threads are using a single C++11 random number generator. The reason for this is that the stream of values is created from a particular large integer seed as a starting point, then each time a number is asked for, the seed is permuted to a new value and a new random value is produced. This process can produce race conditions. (This is also true for the older C rand() function.)
Technically in C++ we can use a separate generator per thread from the C++11 library and ensure that each thread starts from a different seed. When multiple threads or processes start from the same seed, the overall set of random values is not actually random, but the same set repeated on each thread or process. So would need to avoid that.
However, the most accurate fully random stream with the distribution of numbers that we need for real-world applications would start from one seed and ensure that all threads or processes were able to obtain the next random number from the stream when they request one. In this chapter we will see how we can do that using a very good C++ library that has been developed and tested to work correctly and produce the same stream of numbers in sequential code, code using OpenMP directives, and code using MPI.
We will use a library that has been developed separately, is open source, and is maintained regularly. It is called “Tina’s Random Number Generator” or trng. For more details, you can consult the web page for the trng library. You can also look at the open source for the library on gitHub.