The OpenD Programming Language

mir.random.engine

Uniform random engines.

Sections: Convenience • Entropy • Thread-Local • Traits • C Interface

$(TR $(TDNW $(LREF Random)) $(TD Default random number _engine))

$(TR $(TDNW $(LREF rne)) $(TD Per-thread uniquely-seeded instance of default <tt class="inline-code">Random</tt>. Requires <a href="https://en.wikipedia.org/wiki/Thread-local_storage">TLS</a>.))

$(TR $(TDNW $(LREF unpredictableSeed)) $(TD Seed of <tt class="inline-code">size_t</tt> using system entropy. May use <tt class="inline-code">unpredictableSeed!UIntType</tt> for unsigned integers of different sizes.)) $(TR $(TDNW $(LREF genRandomNonBlocking)) $(TD Fills a buffer with system entropy, returning number of bytes copied or negative number on error)) $(TR $(TDNW $(LREF genRandomBlocking)) $(TD Fills a buffer with system entropy, possibly waiting if the system believes it has insufficient entropy. Returns 0 on success.))

$(TR $(TDNW $(LREF EngineReturnType)) $(TD Get return type of random number engine's <tt class="inline-code">opCall()</tt>)) $(TR $(TDNW $(LREF isRandomEngine)) $(TD Check if is random number _engine)) $(TR $(TDNW $(LREF isSaturatedRandomEngine)) $(TD Check if random number engine <tt class="inline-code">G</tt> such that <tt class="inline-code">G.max == EngineReturnType!(G).max</tt>)) $(TR $(TDNW $(LREF preferHighBits)) $(TD Are the high bits of the engine's output known to have better statistical properties than the low bits?))

$(TR $(TDNW $(LREF mir_random_engine_ctor)) $(TD Perform any necessary setup. Automatically called by DRuntime.)) $(TR $(TDNW $(LREF mir_random_engine_dtor)) $(TD Release any resources. Automatically called by DRuntime.)) $(TR $(TDNW $(LREF mir_random_genRandomNonBlocking)) $(TD External name for <a class="xref" href="mir.random.engine.genRandomNonBlocking.2.html">genRandomNonBlocking</a>)) $(TR $(TDNW $(LREF mir_random_genRandomBlocking)) $(TD External name for <a class="xref" href="mir.random.engine.genRandomBlocking.2.html">genRandomBlocking</a>))

<a id="Convenience"></a>Convenience
<a id="Entropy"></a>Entropy
<a id="ThreadLocal"></a>Thread-Local (when TLS enabled)
threadLocal!(Engine)Per-thread uniquely-seeded instance of any specified Engine. Requires TLS.
threadLocalPtr!(Engine)@safe pointer to threadLocal!Engine. Always initializes before return. Warning: do not share between threads!
threadLocalInitialized!(Engine)Explicitly manipulate "is seeded" flag for thread-local instance. Not needed by most library users.
setThreadLocalSeed!(Engine, A...)Initialize thread-local Engine with a known seed rather than a random seed.
<a id="Traits"></a>Traits
<a id="CInterface"></a>C Interface

Modules

linear_congruential
module mir.random.engine.linear_congruential

Linear Congruential generator.

mersenne_twister
module mir.random.engine.mersenne_twister

The Mersenne Twister generator.

pcg
module mir.random.engine.pcg

Permuted Congruential Generator (PCG)

splitmix
module mir.random.engine.splitmix

SplitMix generator family.

xorshift
module mir.random.engine.xorshift
xoshiro
module mir.random.engine.xoshiro

Members

Aliases

Random
alias Random = Mt19937

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.

genRandomBlocking
alias genRandomBlocking = mir_random_genRandomBlocking

Fills a buffer with random data. If not enough entropy has been gathered, it will block.

genRandomNonBlocking
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.

rne
alias rne = threadLocal!Random

Thread-local instance of the default Random allocated and seeded independently for each thread. Requires TLS.

Functions

genRandomBlocking
ptrdiff_t genRandomBlocking(ubyte[] buffer)

Fills a buffer with random data. If not enough entropy has been gathered, it will block.

genRandomNonBlocking
size_t genRandomNonBlocking(ubyte[] buffer)

Fills a buffer with random data. If not enough entropy has been gathered, it won't block. Hence the error code should be inspected.

mir_random_engine_ctor
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.

mir_random_engine_dtor
void mir_random_engine_dtor()

Destructs the mir random seed generators.

mir_random_genRandomBlocking
ptrdiff_t mir_random_genRandomBlocking(void* ptr, size_t len)

Fills a buffer with random data. If not enough entropy has been gathered, it will block.

mir_random_genRandomNonBlocking
size_t mir_random_genRandomNonBlocking(void* ptr, size_t len)

Fills a buffer with random data. If not enough entropy has been gathered, it won't block. Hence the error code should be inspected.

setThreadLocalSeed
void setThreadLocalSeed(A seed)

Sets or resets the seed of threadLocal!Engine using the given arguments. It is not necessary to call this except if you wish to ensure the PRNG uses a known seed.

Properties

threadLocal
Engine threadLocal [@property getter]
threadLocalInitialized
bool threadLocalInitialized [@property getter]
threadLocalPtr
Engine* threadLocalPtr [@property getter]

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.

unpredictableSeed
size_t unpredictableSeed [@property getter]
T unpredictableSeed [@property getter]

A "good" seed for initializing random number engines. Initializing with unpredictableSeed makes engines generate different random number sequences every run.

Templates

EngineReturnType
template EngineReturnType(T)

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

isRandomEngine
template isRandomEngine(T)

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

isSaturatedRandomEngine
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.

preferHighBits
template preferHighBits(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.

Meta

Authors

Ilya Yaroshenko