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 "[]", "$(LPAREN)]", "[$(RPAREN)", 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.

  1. auto uniform(T1 a, T2 b)
  2. auto uniform(T1 a, T2 b, UniformRandomNumberGenerator urng)
    uniform
    (
    string boundaries = "[)"
    T1
    T2
    UniformRandomNumberGenerator
    )
    (
    T1 a
    ,
    T2 b
    ,
    ref UniformRandomNumberGenerator urng
    )
    if (
    isUniformRNG!UniformRandomNumberGenerator
    )
  3. auto uniform(UniformRandomNumberGenerator urng)
  4. auto uniform()
  5. auto uniform(UniformRandomNumberGenerator urng)
  6. auto uniform()

Parameters

a T1

lower bound of the uniform distribution

b T2

upper bound of the uniform distribution

urng UniformRandomNumberGenerator

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

Return Value

Type: auto

A single random variate drawn from the uniform distribution between a and b, whose type is the common type of these parameters

Examples

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);

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;
assert(arr.length == 10);
assert(arr[0] >= 0 && arr[0] < 100);

Meta