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.

# mir.random.engine.xorshift

Generator name | Description |
---|---|

Xorshift1024StarPhi | xorshift1024*φ: when something larger than xoroshiro128+ is needed |

Xorshift64Star32 | xorshift64*/32: internal state of 64 bits and output of 32 bits |

Xorshift32 .. Xorshift160 | Basic `xorshift` generator with n bits of state (32, 64, 96, 128, 160) |

Xorshift192 | Generator from Marsaglia's paper combining 160-bit `xorshift` with a counter |

Xorshift | An alias to one of the generators in this package |

Template name | Description |
---|---|

XorshiftStarEngine | `xorshift` * generator with any word size and any number of bits of state. |

XorshiftEngine | `xorshift` generator with any word size and any number of bits of state. |

License:

Authors:

Masahiro Nakagawa, Ilya Yaroshenko (rework), Nathan Sashihara

- struct
`XorshiftEngine`

(UIntType, uint bits, int sa, int sb, int sc) if (isUnsigned!UIntType); - Xorshift generator. Implemented according to Xorshift RNGs (Marsaglia, 2003) with Sebastino Vigna's optimization for large arrays.Period is 2 ^^ bits - 1 except for a legacy 192-bit uint version (see note below).Parameters:
UIntType Word size of this xorshift generator and the return type of opCall. bits The number of bits of state of this generator. This must be a positive multiple of the size in bits of UIntType. If bits 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 bits == 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.

- enum auto
`isRandomEngine`

; - Marker for mir.random.
`isRandomEngine`

- enum UIntType
`max`

; - Largest generated value.
- pure nothrow @nogc @safe this()(UIntType
`x0`

)

if (UIntType.sizeof > (uint).sizeof);

pure nothrow @nogc @safe this()(uint`x0`

)

if (UIntType.sizeof <= (uint).sizeof); - Constructs a XorshiftEngine generator seeded with
`x0`

. - pure nothrow @nogc @safe UIntType
`opCall`

(); - Advances the random sequence.

- alias
`Xorshift32`

= XorshiftEngine!(uint, 32u, 13, -17, 15).XorshiftEngine;

alias`Xorshift64`

= XorshiftEngine!(uint, 64u, 10, -13, -10).XorshiftEngine;

alias`Xorshift96`

= XorshiftEngine!(uint, 96u, 10, -5, -26).XorshiftEngine;

alias`Xorshift128`

= XorshiftEngine!(uint, 128u, 11, -8, -19).XorshiftEngine;

alias`Xorshift160`

= XorshiftEngine!(uint, 160u, 2, -1, -4).XorshiftEngine;

alias`Xorshift192`

= XorshiftEngine!(uint, 192u, -2, 1, 4).XorshiftEngine;

alias`Xorshift`

= XorshiftEngine!(uint, 128u, 11, -8, -19).XorshiftEngine; - Define XorshiftEngine generators with well-chosen parameters for 32-bit architectures.
`Xorshift`

is an alias of one of the generators in this module.Examples: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);

- struct
`XorshiftStarEngine`

(StateUInt, uint nbits, int sa, int sb, int sc, StateUInt multiplier, OutputUInt = StateUInt) if (isUnsigned!StateUInt && isUnsigned!OutputUInt && (OutputUInt.sizeof <= StateUInt.sizeof) && !(sa > 0 && (sb > 0) && (sc > 0)));

template`XorshiftStarEngine`

(StateUInt, uint nbits, int sa, int sb, int sc, StateUInt multiplier, OutputUInt = StateUInt) if (isUnsigned!StateUInt && isUnsigned!OutputUInt && (OutputUInt.sizeof <= StateUInt.sizeof) && (sa > 0 && (sb > 0) && (sc > 0))) - Template for the xorshift* family of generators (Vigna, 2016; draft 2014).
xorshift* generators are very fast, high-quality PRNGs (pseudorandom number generators) obtained by scrambling the output of a Marsaglia xorshift generator with a 64-bit invertible multiplier (as suggested by Marsaglia in his paper). They are an excellent choice for all non-cryptographic applications, as they are incredibly fast, have long periods and their output passes strong statistical test suites.

Parameters:StateUInt Word size of this xorshift generator. 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. multiplier Output of the internal xorshift engine is multiplied by a constant to eliminate linear artifacts except in the low-order bits. This constant must be an odd number other than 1. OutputUInt Return type of opCall. By default same as StateUInt but can be set to a narrower unsigned type in which case the high bits of the multiplication result are returned. Note If sa, sb, and sc are all positive (which if interpreted as same-direction shifts could not result in a full-period xorshift generator) the shift directions are instead implicitly right-left-right when bits == UIntType.sizeof * 8 and in all other cases left-right-right. This maintains full compatibility with older versions of

`XorshiftStarEngine`

that took all shifts as unsigned magnitudes.- enum auto
`isRandomEngine`

; - Marker for mir.random.
`isRandomEngine`

- enum OutputUInt
`max`

; - Largest generated value.
- enum bool
`preferHighBits`

; - Note that when StateUInt is the same size as OutputUInt the two lowest bits of this generator are LFSRs, and thus will fail binary rank tests. To provide some context,
*every*bit of a Mersenne Twister generator (either the 32-bit or 64-bit variant) is an LFSR.The rand!T functions in mir.random automatically will discard the low bits when generating output smaller than OutputUInt due to this generator having`preferHighBits`

defined true. - pure nothrow @nogc @safe this()(StateUInt
`x0`

); - Constructs a XorshiftStarEngine generator seeded with
`x0`

. - pure nothrow @nogc @safe OutputUInt
`opCall`

()(); - Advances the random sequence.
- pure nothrow @nogc @safe void
`jump`

()()

if (nbits == 1024 && (N == 16) && (sa == 31) && (sb == -11) && (sc == -30)); - This is the
`jump`

function for the standard 1024-bit generator. It is equivalent to 2 ^^ 512 invocations of opCall(); it can be used to generate 2 ^^ 512 non-overlapping subsequences for parallel computations. This function will only be defined if the shifts are the same as for Xorshift1024StarPhi.

- alias
`Xorshift1024StarPhi`

= XorshiftStarEngine!(ulong, 1024u, 31, -11, -30, 11400714819323198483LU, ulong).XorshiftStarEngine; - Define XorshiftStarEngine with well-chosen parameters for large simulations on 64-bit machines.Period of (2 ^^ 1024) - 1, 16-dimensionally equidistributed, and faster and statistically superior to Mt19937_64 while occupying significantly less memory. This generator is recommended for random number generation on 64-bit systems except when 1024 + 32 bits of state are excessive. As described by Vigna in the 2014 draft of his paper published in 2016 detailing the xorshift* family, except with a better multiplier recommended by the author as of 2017-10-08. Public domain reference implementation: xoroshiro.di.unimi.it/xorshift1024star.c.Examples:
import mir.random.engine : EngineReturnType, isSaturatedRandomEngine; auto rnd = Xorshift1024StarPhi(12434UL); auto num = rnd(); assert(num != rnd()); static assert(is(EngineReturnType!Xorshift1024StarPhi == ulong)); static assert(isSaturatedRandomEngine!Xorshift1024StarPhi); //Xorshift1024StarPhi has a jump function that is equivalent //to 2 ^^ 512 invocations of opCall. rnd.jump(); num = rnd(); assert(num != rnd());

- alias
`Xorshift64Star32`

= XorshiftStarEngine!(ulong, 64u, -12, 25, -27, 2685821657736338717LU, uint).XorshiftStarEngine; - Generates 32 bits of output from 64 bits of state. A fast generator with excellent statistical properties for memory-constrained situations where more than 64 bits of state would be too much and generating only 32 bits with each opCall will not cause a slowdown. If you need a generator with 64 bits of state that produces output 64 bits at a time SplitMix64 is an option.Note that xorshift64*/32 is slower than xorshift1024* even when only 32 bits of output are needed at a time. Per Vigna:
The three xor/shifts of a xorshift64* generator must be executed sequentially, as each one is dependent on the result of the previous one. In a xorshift1024* generator two of the xor/shifts are completely independent and can be parallelized internally by the CPU.

Public domain xorshift64* reference implementation (Internet Archive).Examples:import mir.random.engine : isSaturatedRandomEngine; static assert(isSaturatedRandomEngine!Xorshift64Star32); Xorshift64Star32 rnd = Xorshift64Star32(123456789); uint x = rnd(); assert(x == 3988833114);

Copyright © 1999-2018 by the D Language Foundation | Page generated by
Ddoc on Thu Nov 15 18:02:09 2018