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.

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

  1. struct XorshiftEngine(UIntType, uint nbits, int sa, int sb, int sc)
  2. template XorshiftEngine(UIntType, int bits, int a, int b, int c)

Constructors

this
this(UIntType x0)

Constructs a XorshiftEngine generator seeded with x0.

Members

Functions

popFront
void popFront()

Advances the random sequence.

seed
void seed(UIntType x0)

(Re)seeds the generator.

Manifest constants

empty
enum empty;

Always false (random generators are infinite ranges).

Properties

front
UIntType front [@property getter]

Returns the current number in the random sequence.

save
typeof(this) save [@property getter]

Captures a range state.

Variables

isUniformRandom
enum bool isUniformRandom;

Mark this as a Rng

max
enum UIntType max;

Largest generated value.

min
enum UIntType min;

Smallest generated value.

Parameters

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.

Examples

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

Meta