The OpenD Programming Language

uniform01

Generates a uniformly-distributed floating point number of type T in the range [0, 1). If no random number generator is specified, the default RNG rndGen will be used as the source of randomness.

uniform01 offers a faster generation of random variates than the equivalent uniform!"[$(RPAREN)"(0.0, 1.0) and so may be preferred for some applications.

  1. T uniform01()
  2. T uniform01(UniformRNG rng)
    T
    uniform01
    (
    T = double
    UniformRNG
    )
    (
    ref UniformRNG rng
    )
    if ()
    out (result) { assert (0 <= result); assert (result < 1); }

Parameters

rng UniformRNG

(optional) random number generator to use; if not specified, defaults to rndGen

Return Value

Type: T

Floating-point random variate of type T drawn from the uniform distribution across the half-open interval [0, 1).

Examples

import std.math.operations : feqrel;

auto rnd = MinstdRand0(42);

// Generate random numbers in the range in the range [0, 1)
auto u1 = uniform01(rnd);
assert(u1 >= 0 && u1 < 1);

auto u2 = rnd.uniform01!float;
assert(u2 >= 0 && u2 < 1);

// Confirm that the random values with the initial seed 42 are 0.000328707 and 0.524587
assert(u1.feqrel(0.000328707) > 20);
assert(u2.feqrel(0.524587) > 20);

Meta