Report a bug
If you spot a problem with this page, click here to create a Github 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 a local clone.


Xorshift generator.
Masahiro Nakagawa, Ilya Yaroshenko (rework)
struct XorshiftEngine(uint bits, uint a, uint b, uint c) if (isUnsigned!uint);
Xorshift generator using 32bit algorithm. Implemented according to Xorshift RNGs.
bits period
32 2^32 - 1
64 2^64 - 1
96 2^96 - 1
128 2^128 - 1
160 2^160 - 1
192 2^192 - 2^32
enum auto isRandomEngine;
enum uint max;
Largest generated value.
pure nothrow @nogc @safe this(uint x0);
Constructs a XorshiftEngine generator seeded with x0.
pure nothrow @nogc @safe uint opCall();
Advances the random sequence.
alias Xorshift32 = XorshiftEngine!(32u, 13u, 17u, 15u).XorshiftEngine;

alias Xorshift64 = XorshiftEngine!(64u, 10u, 13u, 10u).XorshiftEngine;

alias Xorshift96 = XorshiftEngine!(96u, 10u, 5u, 26u).XorshiftEngine;

alias Xorshift128 = XorshiftEngine!(128u, 11u, 8u, 19u).XorshiftEngine;

alias Xorshift160 = XorshiftEngine!(160u, 2u, 1u, 4u).XorshiftEngine;

alias Xorshift192 = XorshiftEngine!(192u, 2u, 1u, 4u).XorshiftEngine;

alias Xorshift = XorshiftEngine!(128u, 11u, 8u, 19u).XorshiftEngine;
Define XorshiftEngine generators with well-chosen parameters. See each bits examples of "Xorshift RNGs". Xorshift is a Xorshift128's alias because 128bits implementation is mostly used.
import mir.random.engine;
auto rnd = Xorshift(cast(uint)unpredictableSeed);
auto num = rnd();

import std.traits;
static assert(is(ReturnType!rnd == uint));
static assert(isSaturatedRandomEngine!Xorshift);