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

Uniform random engines.
Authors:
Ilya Yaroshenko
template EngineReturnType(T)
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.
nothrow @nogc @property @trusted size_t unpredictableSeed();
A "good" seed for initializing random number engines. Initializing with unpredictableSeed makes engines generate different random number sequences every run.
Returns:
A single unsigned integer seed value, different on each successive call
Examples:
auto rnd = Random(unpredictableSeed);
auto n = rnd();
static assert(is(typeof(n) == size_t));
alias Random = mir.random.engine.mersenne_twister.MersenneTwisterEngine!(ulong, 64LU, 312LU, 156LU, 31LU, 13043109905998158313LU, 29LU, 6148914691236517205LU, 17LU, 8202884508482404352LU, 37LU, 18444473444759240704LU, 43LU, 6364136223846793005LU).MersenneTwisterEngine;
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.
Examples:
import std.traits;
static assert(isSaturatedRandomEngine!Random);
static assert(is(EngineReturnType!Random == size_t));
void mir_random_engine_ctor();
Constructs the mir random seed generators. This constructor needs to be called once before other calls in mir.random.engine.
Automatically called by DRuntime.
void mir_random_engine_dtor();
Destructs the mir random seed generators.
Automatically called by DRuntime.
nothrow @nogc @trusted ptrdiff_t mir_random_genRandomBlocking(void* ptr, size_t len);

alias genRandomBlocking = mir_random_genRandomBlocking;
Fills a buffer with random data. If not enough entropy has been gathered, it will block.
Parameters:
void* ptrpointer to the buffer to fill
size_t lenlength of the buffer (in bytes)
Returns:
A non-zero integer if an error occurred.
Examples:
ubyte[] buf = new ubyte[10];
genRandomBlocking(&buf[0], buf.length);

import std.algorithm.iteration : sum;
assert(buf.sum > 0, "Only zero points generated");
nothrow @nogc @trusted size_t mir_random_genRandomNonBlocking(void* ptr, size_t len);

alias genRandomNonBlocking = mir_random_genRandomNonBlocking;
Fills a buffer with random data. If not enough entropy has been gathered, it won't block. Hence the error code should be inspected.
On Linux >= 3.17 genRandomNonBlocking is guaranteed to succeed for 256 bytes and less.
Parameters:
bufferthe buffer to fill
size_t lenlength of the buffer (in bytes)
Returns:
The number of bytes filled - a negative number of an error occurred
Examples:
ubyte[] buf = new ubyte[10];
genRandomNonBlocking(&buf[0], buf.length);

import std.algorithm.iteration : sum;
assert(buf.sum > 0, "Only zero points generated");