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.

Like std.traits.ReturnType!T but it works even if
T.opCall is a function template.

template isRandomEngine(T)

Test if T is a random engine.
A type should define enum isRandomEngine = true; to be a random engine.

template isSaturatedRandomEngine(T)

Test if T is a saturated random-bit generator.
A random number generator is saturated if T.max == ReturnType!T.max.
A type should define enum isRandomEngine = true; to be a random engine.

template preferHighBits(G) if (isSaturatedRandomEngine!G)

Are the high bits of the engine's output known to have
better statistical properties than the low bits of the
output? This property is set by checking the value of
an optional enum named preferHighBits. If the property
is missing it is treated as false.

This should be specified as true for:

linear congruential generators with power-of-2 modulus

xorshift+ family

xorshift* family

in principle any generator whose final operation is something like
multiplication or addition in which the high bits depend on the low bits
but the low bits are unaffected by the high bits.

The "default", "favorite", "suggested" random number generator type on
the current platform. It is an alias for one of the
generators. You may want to use it if (1) you need to generate some
nice random numbers, and (2) you don't care for the minutiae of the
method being used.

threadLocal!Engine returns a reference to a thread-local instance of
the specified random number generator allocated and seeded uniquely
for each thread. Requires TLS.

threadLocalPtr!Engine is a pointer to the area of thread-local
storage used by threadLocal!Engine. This function is provided because
the compiler can infer it is @safe, unlike &(threadLocal!Engine).
Like threadLocal!Engine this function will auto-initialize the engine.
Do not share pointers returned by threadLocalPtr between
threads!

threadLocalInitialized!Engine is a low-level way to explicitly change
the "initialized" flag used by threadLocal!Engine to determine whether
the Engine needs to be seeded. Setting this to false gives a way of
forcing the next call to threadLocal!Engine to reseed. In general this
is unnecessary but there are some specialized use cases where users have
requested this ability.

import mir.random;
//If you need a pointer to the engine, getting it like this is @safe:
Random* ptr = threadLocalPtr!Random;

Examples:

import mir.random;
import mir.random.engine.xorshift;
//If you need to mark the engine as uninitialized to force a reseed,
//you can do it like this:
threadLocalInitialized!Xorshift1024StarPhi = false;

import mir.random;
import mir.random.engine.xorshift;
alias gen = threadLocal!Xorshift1024StarPhi;
//If you want to you can call the generator's opCall instead of using
//rand!T but it is somewhat clunky because of the ambiguity of
//@property syntax: () looks like optional function parentheses.
staticassert(!__traits(compiles, {ulong x0 = gen();}));//<-- Won't work
staticassert(is(typeof(gen()) == Xorshift1024StarPhi));//<-- because the type is this.
ulong x1 = gen.opCall();//<-- This works though.
ulong x2 = gen()();//<-- This also works.
//But instead of any of those you should really just use gen.rand!T.
ulong x3 = gen.rand!ulong;