Random class
A pseudo-random number generator with excellent statistical properties, and it’s also fast.
Contents
The operator()
method returns the next random integer in the sequence.
The default Random
is initialized using std::random_device
, but it is also possible to
use a dip::uint
seed value when creating the generator to be able to replicate the same pseudo-random
sequence. In multi-threaded code, algorithms can use dip::Random::Split
to split off separate
streams. This causes those algorithms to not replicate the same sequence when run with a different number
of threads. Thus, even if seeded with the same value, the same algorithm can yield different results
when run on a different computer with a different number of cores. To guarantee exact replicability,
run your code single-threaded.
Random
has a 128-bit internal state, and produces 64-bit output with a period of 2128.
On architectures where 128-bit integers are not natively supported, this changes to have a 64-bit internal state,
and produce 32-bit output with a period of 264. This lesser PRNG still has very good statistical
properties. Defining the CMake variable DIP_ALWAYS_128_PRNG
(preprocessor macro DIP_CONFIG_ALWAYS_128_PRNG
)
causes the better 128-bit PRNG engine to be used, using emulated 128-bit arithmetic.
Note that, if DIPlib is compiled with this flag, code that links to it must also be
compiled with this flag, or bad things will happen.
Satisfies the requirements for UniformRandomBitGenerator, and hence can be used in all algorithms of the standard library that require such an object.
Constructors, destructors, assignment and conversion operators
Aliases
- using result_type = Engine::result_type
- The type of the integer returned by the generator.
- using state_type = Engine::state_type
- The type of the internal state of the generator.
Functions
- static auto min() -> dip::Random::result_type constexpr
- The minimum possible value returned by the generator.
- static auto max() -> dip::Random::result_type constexpr
- The maximum possible value returned by the generator.
- void Seed()
- Reseed the random generator using
std::random_device
. - void Seed(dip::uint seed)
- Reseed the random generator using
seed
. - void Advance(dip::uint n)
- Advance the generator
n
steps without producing output, takes log(n
) time. - void SetStream()
- Set the stream for the generator using a random value from the generator itself.
- void SetStream(dip::Random::state_type n)
- Set the stream for the generator to
n
. - auto Split() -> dip::Random
- Create a copy of the random generator, and set it to a random stream. Used by parallel algorithms to provide a different random generator to each thread.
Operators
- auto operator()() -> dip::Random::result_type
- Get the next random value.