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.

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

Constructors

NameDescription
this 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 (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

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.