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.

mir.random.engine.mersenne_twister

The Mersenne Twister generator.
Authors:
Andrei Alexandrescu Ilya Yaroshenko (rework)
struct 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) if (isUnsigned!UIntType);
The Mersenne Twister generator.
enum auto isRandomEngine;
enum UIntType max;
Largest generated value.
enum size_t wordSize;

enum size_t stateSize;

enum size_t shiftSize;

enum size_t maskBits;

enum UIntType xorMask;

enum size_t temperingU;

enum UIntType temperingD;

enum size_t temperingS;

enum UIntType temperingB;

enum size_t temperingT;

enum UIntType temperingC;

enum size_t temperingL;

enum UIntType initializationMultiplier;
Parameters for the generator.
enum UIntType defaultSeed;
The default seed value.
size_t index;
Current reversed payload index with initial value equals to n-1
UIntType[n] data;
Reversed(!) payload.
pure nothrow @nogc @safe this(UIntType value);
Constructs a MersenneTwisterEngine object.
pure nothrow @nogc @safe this()(scope const(UIntType)[] array);
Constructs a MersenneTwisterEngine object.
Note that MersenneTwisterEngine([123]) will not result in the same initial state as MersenneTwisterEngine(123).
pure nothrow @nogc @safe UIntType opCall();
Advances the generator.
alias Mt19937 = MersenneTwisterEngine!(uint, 32LU, 624LU, 397LU, 31LU, 2567483615u, 11LU, 4294967295u, 7LU, 2636928640u, 15LU, 4022730752u, 18LU, 1812433253u).MersenneTwisterEngine;
A MersenneTwisterEngine instantiated with the parameters of the original engine MT19937, generating uniformly-distributed 32-bit numbers with a period of 2 to the power of 19937.
This is recommended for random number generation on 32-bit systems unless memory is severely restricted, in which case a Xorshift would be the generator of choice.
Examples:
import mir.random.engine;

// bit-masking by generator maximum is necessary
// to handle 64-bit `unpredictableSeed`
auto gen = Mt19937(unpredictableSeed & Mt19937.max);
auto n = gen();

import std.traits;
static assert(is(ReturnType!gen == uint));
alias Mt19937_64 = MersenneTwisterEngine!(ulong, 64LU, 312LU, 156LU, 31LU, 13043109905998158313LU, 29LU, 6148914691236517205LU, 17LU, 8202884508482404352LU, 37LU, 18444473444759240704LU, 43LU, 6364136223846793005LU).MersenneTwisterEngine;
A MersenneTwisterEngine instantiated with the parameters of the original engine MT19937, generating uniformly-distributed 64-bit numbers with a period of 2 to the power of 19937.
This is recommended for random number generation on 64-bit systems unless memory is severely restricted, in which case a Xorshift would be the generator of choice.
Examples:
import mir.random.engine;

auto gen = Mt19937_64(unpredictableSeed);
auto n = gen();

import std.traits;
static assert(is(ReturnType!gen == ulong));