View source code
Display the source code in std/random.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla 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 local clone.

Struct std.random.LinearCongruentialEngine

Linear Congruential generator. When m = 0, no modulus is used.

struct LinearCongruentialEngine(UIntType, UIntType a, UIntType c, UIntType m)
  
if (isUnsigned!UIntType);

Constructors

NameDescription
this (x0) Constructs a LinearCongruentialEngine generator seeded with x0.

Properties

NameTypeDescription
front[get] UIntTypeReturns the current number in the random sequence.
save[get] typeof(this)

Methods

NameDescription
popFront () Advances the random sequence.
seed (x0) (Re)seeds the generator.

Example

Declare your own linear congruential engine

alias CPP11LCG = LinearCongruentialEngine!(uint, 48271, 0, 2_147_483_647);

// seed with a constant
auto rnd = CPP11LCG(42);
auto n = rnd.front; // same for each run
writeln(n); // 2027382

Example

Declare your own linear congruential engine

// glibc's LCG
alias GLibcLCG = LinearCongruentialEngine!(uint, 1103515245, 12345, 2_147_483_648);

// Seed with an unpredictable value
auto rnd = GLibcLCG(unpredictableSeed);
auto n = rnd.front; // different across runs

Example

Declare your own linear congruential engine

// Visual C++'s LCG
alias MSVCLCG = LinearCongruentialEngine!(uint, 214013, 2531011, 0);

// seed with a constant
auto rnd = MSVCLCG(1);
auto n = rnd.front; // same for each run
writeln(n); // 2745024

Authors

Andrei Alexandrescu Masahiro Nakagawa (Xorshift random generator) Joseph Rushton Wakeling (Algorithm D for random sampling) Ilya Yaroshenko (Mersenne Twister implementation, adapted from mir-random)

License

Boost License 1.0.