Xoroshiro128plus

The xoroshiro128+ uniform random number generator is the fastest full-period generator passing the BigCrush test suite without systematic failures.

Due to the relatively short period (2 ^^ 128 - 1) it is acceptable only for applications with a mild amount of parallelism; for applications requiring many parallel random sequences, Xorshift1024star is recommended instead.

Besides passing BigCrush, this generator passes the PractRand test suite up to (and including) 16TB, with the exception of binary rank tests, which fail due to the lowest bit being a linear feedback shift register (LFSR). All other bits pass all tests.

Constructors

this
this()
Undocumented in source.
this
this(ulong s)
this(ulong s0, ulong s1)
this(ulong[2] s)
this(R range)

Constructor (RNG instances can only be initialized with a specified seed).

Postblit

this(this)
this(this)
Undocumented in source.

Members

Functions

dup
typeof(this) dup()

Provides a copy of this RNG instance with identical internal state.

front
ulong front()

Range primitives

jump
void jump()

Jump function, equivalent to 2 ^^ 64 calls to popFront(); can be used to generate 2 ^^ 64 non-overlapping subsequences for parallel computation.

popFront
void popFront()

Range primitives

seed
void seed(ulong s)
void seed(ulong s0, ulong s1)
void seed(ulong[2] s)
void seed(R range)

(Re)seeds the generator.

Variables

empty
enum bool empty;

Range primitives

isUniformRandom
enum bool isUniformRandom;

Marks this range as a uniform random number generator

max
enum ulong max;

Largest generated value

min
enum ulong min;

Smallest generated value (0)

Examples

import std.array : array;
import std.random : isUniformRNG, randomSample, uniform;
import std.range : iota, take;
import dxorshift.xoroshiro128plus;

// xoroshiro128+ generators must be initialized
// with a specified seed
auto gen = Xoroshiro128plus(123456);

// verify it is indeed a uniform RNG as defined
// in the standard library, whether accessed
// directly or via a pointer
static assert(isUniformRNG!(typeof(gen)));
static assert(isUniformRNG!(typeof(&gen)));

// since the postblit is disabled, we must
// pass a pointer to any functionality that
// would otherwise copy the RNG by value
assert((&gen).take(2).array == [14854895758870614632uL,
                                2102156639392820999uL]);

// this means, of course, that we must guarantee
// the lifetime of the pointer is valid for the
// lifetime of any functionality that uses it
auto sample = iota(100).randomSample(10, &gen).array;

// however, we can pass the RNG as-is to any
// functionality that takes it by ref and does
// not try to copy it by value
auto val = uniform!"(]"(3.5, 4.0, gen);

// in circumstances where we really want to
// copy the RNG state, we can use `dup`
auto gen2 = gen.dup;
assert((&gen).take(9).array == (&gen2).take(9).array);

Meta

Credits

This code is ported from the public-domain reference implementation by David Blackman and Sebastiano Vigna, available online at http://xorshift.di.unimi.it/xoroshiro128plus.c