hap.random.generator

Implements algorithms for uniform pseudo-random number generation. Following the definition used in the C++11 Standard Template Library $(LESS)_random$(GREATER) header, a uniform random number generator is defined to be an Input Range whose values are unsigned integer values drawn from the closed interval [min, max], such that each value among the possible set of results has (ideally) equal probability of being next in the sequence. Pseudo-random number generators are typically implemented as Forward Ranges, but this is not a requirement.

The uniform random number generators provided by this module are implemented as final classes to ensure reference type semantics. These semantics are assumed by all other functions in the package, so user-defined value-type RNGs may fail in unexpected ways.

Non-deterministic random number generators, or random devices, are provided in a separate module (currently experimental).

Members

Aliases

MinstdRand
alias MinstdRand = LinearCongruentialEngine!(uint, 48271, 0, 2147483647)
Undocumented in source.
MinstdRand0
alias MinstdRand0 = LinearCongruentialEngine!(uint, 16807, 0, 2147483647)

LinearCongruentialEngine generators with well-chosen parameters. MinstdRand0 implements Park and Miller's "minimal standard" generator, which uses 16807 for the multiplier. MinstdRand implements a variant that has slightly better spectral behaviour by using the multiplier 48271. Both generators are rather simplistic.

Mt11213b
alias Mt11213b = MersenneTwisterEngine!(uint, 32, 351, 175, 19, 0xccab8ee7U, 11, 0xffffffffU, 7, 0x31b6ab00U, 15, 0xffe50000U, 17, 1812433253U)
Mt19937
alias Mt19937 = MersenneTwisterEngine!(uint, 32, 624, 397, 31, 0x9908b0dfU, 11, 0xffffffffU, 7, 0x9d2c5680U, 15, 0xefc60000U, 18, 1812433253U)
Mt19937_64
alias Mt19937_64 = MersenneTwisterEngine!(ulong, 64, 312, 156, 31, 0xb5026f5aa96619e9UL, 29, 0x5555555555555555UL, 17, 0x71d67fffeda60000UL, 37, 0xfff7eee000000000UL, 43, 6364136223846793005UL)

Mersenne Twister generators with well-chosen parameters. Mt11213b offers a generator with a period of 2^11213 - 1 and a 32-bit datatype, while Mt19937 and Mt19937_64 offer generators with 32- and 64-bit datatypes respectively, both having a period of 2^19937 - 1. The three generators offer a good uniform distribution in up to 350, 623 and 311 dimensions respectively. Mt19937 is the most typical configuration, widely used in many different programming languages as a high-quality default random number generator.

Random
alias Random = Mt19937

The "default", "recommended" RNG type for the current platform, implemented as an alias to one of the generators defined elsewhere in this module. Its use is suggested if you want to generate good-quality random numbers without caring about the minutiae of the method being used. The default thread-local RNG instance rndGen is of type Random.

UniformRNGTypes
alias UniformRNGTypes = TypeTuple!(MinstdRand0, MinstdRand, Mt11213b, Mt19937, Mt19937_64, Xorshift32, Xorshift64, Xorshift128, Xorshift160, Xorshift192)

TypeTuple of all uniform RNGs defined in this module.

Xorshift
alias Xorshift = Xorshift128
Undocumented in source.
Xorshift128
alias Xorshift128 = XorshiftEngine!(uint, 128, 11, 8, 19)
Undocumented in source.
Xorshift160
alias Xorshift160 = XorshiftEngine!(uint, 160, 2, 1, 4)
Undocumented in source.
Xorshift192
alias Xorshift192 = XorshiftEngine!(uint, 192, 2, 1, 4)
Undocumented in source.
Xorshift32
alias Xorshift32 = XorshiftEngine!(uint, 32, 13, 17, 15)

Define XorshiftEngine generators with well-chosen parameters as provided by Marsaglia (2003). The default provided by Xorshift corresponds to the 128-bit generator as an optimal balance of statistical quality and speed.

Xorshift64
alias Xorshift64 = XorshiftEngine!(uint, 64, 10, 13, 10)
Undocumented in source.
Xorshift96
alias Xorshift96 = XorshiftEngine!(uint, 96, 10, 5, 26)
Undocumented in source.

Classes

LinearCongruentialEngine
class LinearCongruentialEngine(UIntType, UIntType a, UIntType c, UIntType m)

Linear congruential generators are some of the oldest algorithms for generating pseudo-random numbers. They tend to be fast but not of particularly high statistical quality, so their use is recommended only in very constrained circumstances, e.g. where memory is very severely restricted. Even then, consider using an Xorshift generator instead, as this should provide much higher statistical quality.

MersenneTwisterEngine
class MersenneTwisterEngine(UIntType, size_t w, size_t n, size_t m, size_t r, UIntType a, size_t u, UIntType d, size_t s, UIntType b, size_t t, UIntType c, size_t l, UIntType f)

The Mersenne Twister generator, developed by Makoto Matsumoto and Takuji Nishimura (1997), allows for fast generation of high-quality pseudorandom numbers, and is widely used as a default random number generator by many programming languages, including D. The current implementation is adapted from that of Boost.Random and supports both 32- and 64-bit datatypes.

XorshiftEngine
class XorshiftEngine(UIntType, UIntType bits, UIntType a, UIntType b, UIntType c)

The Xorshift family of generators, developed by George Marsaglia (2003), offer high-quality random number generation with minimal storage requirements and computational cost. They are therefore highly suitable for use in low-memory environments or slower processors. The current implementation supports Xorshift random number generation with a 32-bit datatype only.

Functions

rndGen
Random rndGen()

Global random number generator used by various functions in this package whenever no other generator is specified. It is allocated per-thread and initialized with a different unpredictable seed for each thread.

unpredictableSeed
uint unpredictableSeed()

A "good" seed for initializing random number generators. Initializing with unpredictableSeed ensures that RNGs produce different pseudo-random sequences each time they are run.

Meta

Credits

The Mersenne Twister implementation is adapted from the C++ implementation in Boost.Random by Jens Maurer and Steven Watanabe, similarly licensed under the Boost Software License 1.0.

Source

See Source File
$(HAPSRC hap/random/_generator.d)

Authors

Andrei Alexandrescu, Masahiro Nakagawa (Xorshift random generator), Joseph Rushton Wakeling