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

Basic API to construct non-uniform random number generators and stochastic algorithms. Non-unoform and uniform random variable can be found at mir.random.variable.

Generation functions

Function Name Description
rand Generates real, integral, boolean, and enumerated uniformly distributed values.
randIndex Generates uniformly distributed index.
randGeometric Generates geometric distribution with p = 1/2.
randExponential2 Generates scaled Exponential distribution.
Publicly includes mir.random.engine.
Authors:
Ilya Yaroshenko
T rand(T, G)(ref G gen)
if (isSaturatedRandomEngine!G && isIntegral!T && !is(T == enum));
Parameters:
G gensaturated random number generator
Returns:
Uniformly distributed integer for interval [0 .. T.max].
Examples:
import mir.random.engine.xorshift;
auto gen = Xorshift(1);
auto s = gen.rand!short;
auto n = gen.rand!ulong;
bool rand(T : bool, G)(ref G gen)
if (isSaturatedRandomEngine!G);
Parameters:
G gensaturated random number generator
Returns:
Uniformly distributed boolean.
Examples:
import mir.random.engine.xorshift;
auto gen = Xorshift(1);
auto s = gen.rand!bool;
T rand(T, G)(ref G gen)
if (isSaturatedRandomEngine!G && is(T == enum));
Parameters:
G gensaturated random number generator
Returns:
Uniformly distributed enumeration.
Examples:
import mir.random.engine.xorshift;
auto gen = Xorshift(1);
enum A { a, b, c }
auto e = gen.rand!A;
Examples:
import mir.random.engine.xorshift;
auto gen = Xorshift(1);
enum A : dchar { a, b, c }
auto e = gen.rand!A;
Examples:
import mir.random.engine.xorshift;
auto gen = Xorshift(1);
enum A : string { a = "a", b = "b", c = "c" }
auto e = gen.rand!A;
T rand(T, G)(ref G gen, sizediff_t boundExp = 0)
if (isSaturatedRandomEngine!G && isFloatingPoint!T);
Parameters:
G gensaturated random number generator
sizediff_t boundExpbound exponent (optional). boundExp must be less or equal to T.max_exp.
Returns:
Uniformly distributed real for interval (-2^^boundExp , 2^^boundExp).

Notefabs can be used to get a value from positive interval [0, 2^^boundExp).

Examples:
import std.math: fabs;
import mir.random.engine.xorshift;
auto gen = Xorshift(1);

auto a = gen.rand!float;
assert(-1 < a && a < +1);

auto b = gen.rand!double(4);
assert(-16 < b && b < +16);

auto c = gen.rand!double(-2);
assert(-0.25 < c && c < +0.25);

auto d = gen.rand!real.fabs;
assert(0.0L <= d && d < 1.0L);
Examples:
Subnormal numbers
import mir.random.engine.xorshift;
auto gen = Xorshift(1);
auto x = gen.rand!double(double.min_exp-1);
assert(-double.min_normal < x && x < double.min_normal);
T randIndex(T, G)(ref G gen, T m)
if (isSaturatedRandomEngine!G && isUnsigned!T);
Parameters:
G genuniform random number generator
T mpositive module
Returns:
Uniformly distributed integer for interval [0 .. m).
Examples:
import mir.random.engine.xorshift;
auto gen = Xorshift(1);
auto s = gen.randIndex!uint(100);
auto n = gen.randIndex!ulong(-100);
size_t randGeometric(G)(ref G gen)
if (isSaturatedRandomEngine!G);
Returns:
n >= 0 such that P(n) := 1 / (2^^(n + 1)).
T randExponential2(T, G)(ref G gen)
if (isSaturatedRandomEngine!G && isFloatingPoint!T);
Parameters:
G gensaturated random number generator
Returns:
X ~ Exp(1) / log(2).

Notefabs can be used to get a value from positive interval [0, 2^^boundExp).

Examples:
import mir.random.engine.xorshift;
auto gen = Xorshift(cast(uint)unpredictableSeed);
auto v = gen.randExponential2!double();