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.XorshiftEngine

Xorshift generator. Implemented according to Xorshift RNGs (Marsaglia, 2003) when the size is small. For larger sizes the generator uses Sebastino Vigna's optimization of using an index to avoid needing to rotate the internal array.

struct XorshiftEngine(UIntType, uint nbits, int sa, int sb, int sc)
  
if (isUnsigned!UIntType && !(sa > 0 && (sb > 0) && (sc > 0)));
alias XorshiftEngine(UIntType, int bits, int a, int b, int c) = XorshiftEngine!(UIntType,bits,a,-b,c);

Period is 2 ^^ nbits - 1 except for a legacy 192-bit uint version (see note below).

Struct XorshiftEngine

Constructors

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

Properties

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

Methods

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

Alias XorshiftEngine

Parameters

NameDescription
UIntType Word size of this xorshift generator and the return type of opCall.
nbits The number of bits of state of this generator. This must be a positive multiple of the size in bits of UIntType. If nbits is large this struct may occupy slightly more memory than this so it can use a circular counter instead of shifting the entire array.
sa The direction and magnitude of the 1st shift. Positive means left, negative means right.
sb The direction and magnitude of the 2nd shift. Positive means left, negative means right.
sc The direction and magnitude of the 3rd shift. Positive means left, negative means right.

Note

For historical compatibility when nbits == 192 and UIntType is uint a legacy hybrid PRNG is used consisting of a 160-bit xorshift combined with a 32-bit counter. This combined generator has period equal to the least common multiple of 2^^160 - 1 and 2^^32.

Previous versions of XorshiftEngine did not provide any mechanism to specify the directions of the shifts, taking each shift as an unsigned magnitude. For backwards compatibility, because three shifts in the same direction cannot result in a full-period XorshiftEngine, when all three of sa, sb, sc, are positive XorshiftEngine` treats them as unsigned magnitudes and uses shift directions to match the old behavior of XorshiftEngine.

Not every set of shifts results in a full-period xorshift generator. The template does not currently at compile-time perform a full check for maximum period but in a future version might reject parameters resulting in shorter periods.

Example

alias Xorshift96  = XorshiftEngine!(uint, 96,  10, 5,  26);
auto rnd = Xorshift96(42);
auto num = rnd.front;  // same for each run
writeln(num); // 2704588748

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.