Unpredictable random numbers
This pattern is licensed under the CC0 Public Domain Dedication.
Seed a random number engine with greater unpredictability.
A pseudo-random number engine is initialised with a seed, which determines the initial state of the engine. When seeding with a single 32 bit value (as is typically provided by std::random_device), the engine can be in one of only 232 states. By providing an an engine with a greater amount of random seed data, we increase the number of possible states and therefore the unpredicitability of the engine.
On line 4, we create a
std::random_device as a source of
non-deterministic random numbers (if such a source is available).
On line 5, we initialise a
with several initial seeds produced by the
On line 7, we seed a random number engine with this
which provides the engine with a sequence of evenly
distributed seeds with low bias (although not
Providing more initial seeds will increase the unpredictability of
the engine by increasing the number of possible seed sequences that
might be generated.
std::seed_seq can be used to initialize multiple random
number engines, bear in mind that a particular
produce the same sequence of seeds each time it is used.
Note: An unpredictable random number engine is not necessarily cryptographically secure.