Initializing all bits of internal state of a pseudo-random number generator
In the previous recipe, we looked at the pseudo-random number library, along with its components, and how it can be used to produce numbers in different statistical distributions. One important factor that was overlooked in that recipe is the proper initialization of the pseudo-random number generators.
With careful analysis (that is beyond the purpose of this recipe or this book), it can be shown that the Mersenne twister engine has a bias toward producing some values repeatedly and omitting others, thus generating numbers not in a uniform distribution, but rather in a binomial or Poisson distribution. In this recipe, you will learn how to initialize a generator in order to produce pseudo-random numbers with a true uniform distribution.
Getting ready
You should read the previous recipe, Generating pseudo-random numbers, to get an overview of what the pseudo-random number library offers.
How to do it...
To properly initialize a pseudo-random number generator to produce a uniformly distributed sequence of pseudo-random numbers, perform the following steps:
- Use an
std::random_device
to produce random numbers to be used as seeding values:std::random_device rd;
- Generate random data for all internal bits of the engine:
std::array<int, std::mt19937::state_size> seed_data {}; std::generate(std::begin(seed_data), std::end(seed_data), std::ref(rd));
- Create an
std::seed_seq
object from the previously generated pseudo-random data:std::seed_seq seq(std::begin(seed_data), std::end(seed_data));
- Create an engine object and initialize all the bits representing the internal state of the engine; for example, an
mt19937
has 19,937 bits of internal states:auto eng = std::mt19937{ seq };
- Use the appropriate distribution based on the requirements of the application:
auto dist = std::uniform_real_distribution<>{ 0, 1 };
How it works...
In all the examples shown in the previous recipe, we used the std::mt19937
engine to produce pseudo-random numbers. Though the Mersenne twister is slower than the other engines, it can produce the longest sequences of non-repeating numbers with the best spectral characteristics. However, initializing the engine in the manner shown in the previous recipe will not have this effect. The problem is that the internal state of mt19937
has 624 32-bit integers, and in the examples from the previous recipe, we have only initialized one of them.
When working with the pseudo-random number library, remember the following rule of thumb (shown in the information box):
In order to produce the best results, engines must have all their internal state properly initialized before generating numbers.
The pseudo-random number library provides a class for this particular purpose, called std::seed_seq
. This is a generator that can be seeded with any number of 32-bit integers and produces the requested number of integers evenly distributed in the 32-bit space.
In the preceding code from the How to do it... section, we defined an array called seed_data
with a number of 32-bit integers equal to the internal state of the mt19937
generator; that is, 624 integers. Then, we initialized the array with random numbers produced by std::random_device
. The array was later used to seed std::seed_seq
, which, in turn, was used to seed the mt19937
generator.
See also
- Generating pseudo-random numbers to familiarize yourself with the capabilities of the standard numerics library for generating pseudo-random numbers