Report a bug
If you spot a problem with this page, click here to create a GitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.


Linear Congruential generator.
Andrei Alexandrescu Ilya Yaroshenko (rework)
struct LinearCongruentialEngine(Uint, Uint a, Uint c, Uint m) if (isUnsigned!Uint);
Linear Congruential generator.
enum auto isRandomEngine;
enum Uint max;
Highest generated value (modulus - 1 - bool(c == 0)).
enum Uint multiplier;

enum Uint increment;

enum Uint modulus;
The parameters of this distribution. The random number is x = (x * multiplier + increment) % modulus.
enum bool preferHighBits;
The low bits of a linear congruential generator whose modulus is a power of 2 have a much shorter period than the high bits. Note that for LinearCongruentialEngine, modulus == 0 signifies a modulus of 2 ^^ (Uint.sizeof*8) which is not representable as Uint.
pure nothrow @nogc @safe this(Uint x0);
Constructs a LinearCongruentialEngine generator seeded with x0.
Uint x0 seed, must be positive if c equals to 0.
pure nothrow @nogc @safe Uint opCall();
Advances the random sequence.
alias MinstdRand0 = LinearCongruentialEngine!(uint, 16807u, 0u, 2147483647u).LinearCongruentialEngine;

alias MinstdRand = LinearCongruentialEngine!(uint, 48271u, 0u, 2147483647u).LinearCongruentialEngine;
Define LinearCongruentialEngine generators with well-chosen parameters. MinstdRand0 implements Park and Miller's "minimal standard" generator that uses 16807 for the multiplier. MinstdRand implements a variant that has slightly better spectral behavior by using the multiplier 48271. Both generators are rather simplistic.
import mir.random.engine;
// seed with a constant
auto rnd0 = MinstdRand0(1);
auto n = rnd0(); // same for each run
// Seed with an unpredictable value
rnd0 = MinstdRand0(cast(uint)unpredictableSeed);
n = rnd0(); // different across runs

import std.traits;
static assert(is(ReturnType!rnd0 == uint));