Index of /examples/random_numbers

Icon  Name                    Last modified      Size  Description
[DIR] Parent Directory - [DIR] xoroshiro128_plus/ 30-Sep-2016 10:55 -
Random Number Generation Best Practices

Random Number Generation Best Practices


The generation of random numbers in computers is a well-researched area of computer science. In the context of science and engineering, the use of random numbers in simulations and other programs typically involves the use of pseudo-random numbers. Pseudo-random numbers are sequences of numbers that approximate truly random numbers based on a repeated computations starting from an initial state. The purpose of these examples is to present an example pseudo-random number generator (RNG) that is suitable for any purpose except for very large scale programs involving hundreds or thousands of CPUs.

A poor choice of RNG algorithm could lead to results of questionable quality!

Click here to jump to recommended RNGs for science and engineering applications.

Some relevant publications

Here are some journal articles relevant to this subject for motivation. The full text is available at the links:

What to look for in an RNG

Here are some rules of thumb for choosing an RNG for your project, from the viewpoint of science and engineering software. A good RNG algorithm should have at least the following characteristics:

What to AVOID in an RNG

Again, this list is oriented towards science and engineering software. The types of RNGs listed here are adequate for many other purposes.

Recommended RNGs for several languages

Here are a few recommended RNG algorithms.

Seeding a RNG

Every computational RNG requires a starting value. There are many ways to generate random starting seeds. For an example of how to use the Linux /dev/random hardware RNG utility that is available on the SCC computers, see the C code example in this directory. Another way is to retrieve some seed values from hardware-based random number generators available on the Internet. has an HTTP-based API to pull random bytes based on atmospheric radio noise, and HotBits offers a web form to download random bytes based on radioactive decay.

Seeding from the current system clock value is another way to seed an RNG. However, if multiple processes (ex. MPI) or threads (ex. OpenMP) on a computer are seeding separate RNGs it is entirely possible for some or all of them to grab the same clock value, depending on the resolution of the clock and when the value is retrieved. A better approach is to seed a single RNG from the clock and then use its sequence of numbers to provide seeds to the different processes or threads. Another approach would be to use an RNG like the PCG or xoroshiro128+ generators that can be "jumped" ahead by a large amount (say 264 numbers) based on a process or thread id. This provides each process or thread with their own separate sequence of numbers based on a single seed value. For an example, see the C++ and C sample codes for the xoroshiro128+ generator in this directory.

Document written by Brian Gregor on 9/28/2016. Last modified on 9/29/2016.