std.random.uniform
- multiple declarations
Function uniform
Generates a number between a
and b
. The boundaries
parameter controls the shape of the interval (open vs. closed on
either side). Valid values for boundaries
are "[]"
, "(]"
, "[)"
, and "()"
. The default interval
is closed to the left and open to the right. The version that does not
take urng
uses the default generator rndGen
.
auto uniform(string boundaries = "[)", T1, T2)(
T1 a,
T2 b
)
if (!is(CommonType!(T1, T2) == void));
auto uniform(string boundaries = "[)", T1, T2, UniformRandomNumberGenerator)(
T1 a,
T2 b,
ref UniformRandomNumberGenerator urng
)
if (isFloatingPoint!(CommonType!(T1, T2)) && isUniformRNG!UniformRandomNumberGenerator);
auto uniform(string boundaries = "[)", T1, T2, RandomGen)(
T1 a,
T2 b,
ref RandomGen rng
)
if ((isIntegral!(CommonType!(T1, T2)) || isSomeChar!(CommonType!(T1, T2))) && isUniformRNG!RandomGen);
Parameters
Name | Description |
---|---|
a | lower bound of the uniform distribution |
b | upper bound of the uniform distribution |
urng | (optional) random number generator to use;
if not specified, defaults to rndGen |
Returns
A single random variate drawn from the uniform distribution
between a
and b
, whose type is the common type of
these parameters
Example
auto rnd = Random(unpredictableSeed);
// Generate an integer in [0, 1023]
auto a = uniform(0, 1024, rnd);
assert(0 <= a && a < 1024);
// Generate a float in [0, 1)
auto b = uniform(0.0f, 1.0f, rnd);
assert(0 <= b && b < 1);
// Generate a float in [0, 1]
b = uniform!"[]"(0.0f, 1.0f, rnd);
assert(0 <= b && b <= 1);
// Generate a float in (0, 1)
b = uniform!"()"(0.0f, 1.0f, rnd);
assert(0 < b && b < 1);
Example
Create an array of random numbers using range functions and UFCS
import std .array : array;
import std .range : generate, takeExactly;
int[] arr = generate!(() => uniform(0, 100)) .takeExactly(10) .array;
writeln(arr .length); // 10
assert(arr[0] >= 0 && arr[0] < 100);
Example
import std .conv : to;
import std .meta : AliasSeq;
import std .range .primitives : isForwardRange;
import std .traits : isIntegral, isSomeChar;
auto gen = Mt19937(123_456_789);
static assert(isForwardRange!(typeof(gen)));
auto a = uniform(0, 1024, gen);
assert(0 <= a && a <= 1024);
auto b = uniform(0.0f, 1.0f, gen);
assert(0 <= b && b < 1, to!string(b));
auto c = uniform(0.0, 1.0);
assert(0 <= c && c < 1);
static foreach (T; AliasSeq!(char, wchar, dchar, byte, ubyte, short, ushort,
int, uint, long, ulong, float, double, real))
{{
T lo = 0, hi = 100;
// Try tests with each of the possible bounds
{
T init = uniform(lo, hi);
size_t i = 50;
while (--i && uniform(lo, hi) == init) {}
assert(i > 0);
}
{
T init = uniform!"[)"(lo, hi);
size_t i = 50;
while (--i && uniform(lo, hi) == init) {}
assert(i > 0);
}
{
T init = uniform!"(]"(lo, hi);
size_t i = 50;
while (--i && uniform(lo, hi) == init) {}
assert(i > 0);
}
{
T init = uniform!"()"(lo, hi);
size_t i = 50;
while (--i && uniform(lo, hi) == init) {}
assert(i > 0);
}
{
T init = uniform!"[]"(lo, hi);
size_t i = 50;
while (--i && uniform(lo, hi) == init) {}
assert(i > 0);
}
/* Test case with closed boundaries covering whole range
* of integral type
*/
static if (isIntegral!T || isSomeChar!T)
{
foreach (immutable _; 0 .. 100)
{
auto u = uniform!"[]"(T .min, T .max);
static assert(is(typeof(u) == T));
assert(T .min <= u, "Lower bound violation for uniform!\"[]\" with " ~ T .stringof);
assert(u <= T .max, "Upper bound violation for uniform!\"[]\" with " ~ T .stringof);
}
}
}}
auto reproRng = Xorshift(239842);
static foreach (T; AliasSeq!(char, wchar, dchar, byte, ubyte, short,
ushort, int, uint, long, ulong))
{{
T lo = T .min + 10, hi = T .max - 10;
T init = uniform(lo, hi, reproRng);
size_t i = 50;
while (--i && uniform(lo, hi, reproRng) == init) {}
assert(i > 0);
}}
{
bool sawLB = false, sawUB = false;
foreach (i; 0 .. 50)
{
auto x = uniform!"[]"('a', 'd', reproRng);
if (x == 'a') sawLB = true;
if (x == 'd') sawUB = true;
assert('a' <= x && x <= 'd');
}
assert(sawLB && sawUB);
}
{
bool sawLB = false, sawUB = false;
foreach (i; 0 .. 50)
{
auto x = uniform('a', 'd', reproRng);
if (x == 'a') sawLB = true;
if (x == 'c') sawUB = true;
assert('a' <= x && x < 'd');
}
assert(sawLB && sawUB);
}
{
bool sawLB = false, sawUB = false;
foreach (i; 0 .. 50)
{
immutable int lo = -2, hi = 2;
auto x = uniform!"()"(lo, hi, reproRng);
if (x == (lo+1)) sawLB = true;
if (x == (hi-1)) sawUB = true;
assert(lo < x && x < hi);
}
assert(sawLB && sawUB);
}
{
bool sawLB = false, sawUB = false;
foreach (i; 0 .. 50)
{
immutable ubyte lo = 0, hi = 5;
auto x = uniform(lo, hi, reproRng);
if (x == lo) sawLB = true;
if (x == (hi-1)) sawUB = true;
assert(lo <= x && x < hi);
}
assert(sawLB && sawUB);
}
{
foreach (i; 0 .. 30)
{
writeln(i); // uniform(i, i + 1, reproRng)
}
}
Function uniform
Generates a uniformly-distributed number in the range [T
for any integral or character type T
. If no random
number generator is passed, uses the default rndGen
.
auto uniform(T, UniformRandomNumberGenerator)(
ref UniformRandomNumberGenerator urng
)
if (!is(T == enum) && (isIntegral!T || isSomeChar!T) && isUniformRNG!UniformRandomNumberGenerator);
auto uniform(T)()
if (!is(T == enum) && (isIntegral!T || isSomeChar!T));
auto uniform(E, UniformRandomNumberGenerator)(
ref UniformRandomNumberGenerator urng
)
if (is(E == enum) && isUniformRNG!UniformRandomNumberGenerator);
auto uniform(E)()
if (is(E == enum));
If an enum
is used as type, the random variate is drawn with
equal probability from any of the possible values of the enum E
.
Parameters
Name | Description |
---|---|
urng | (optional) random number generator to use;
if not specified, defaults to rndGen |
Returns
Random variate drawn from the uniform distribution across all
possible values of the integral, character or enum type T
.
Example
auto rnd = MinstdRand0(42);
writeln(rnd .uniform!ubyte); // 102
writeln(rnd .uniform!ulong); // 4838462006927449017
enum Fruit { apple, mango, pear }
version (D_LP64) // https://issues.dlang.org/show_bug.cgi?id=15147
writeln(rnd .uniform!Fruit); // Fruit.mango
Authors
Andrei Alexandrescu Masahiro Nakagawa (Xorshift random generator) Joseph Rushton Wakeling (Algorithm D for random sampling) Ilya Yaroshenko (Mersenne Twister implementation, adapted from mir-random)