choice

Returns a random, uniformly chosen, element e from the supplied Range range. If no random number generator is passed, the default rndGen is used.

  1. auto ref choice(Range range, RandomGen urng)
    ref
    choice
    (
    Range
    RandomGen = Random
    )
    (
    Range range
    ,
    ref RandomGen urng
    )
    if (
    hasLength!Range
    &&
    isUniformRNG!RandomGen
    )
  2. auto ref choice(Range range)
  3. auto ref choice(Range range, RandomGen urng)
  4. auto ref choice(Range range)

Parameters

range Range

a random access range that has the length property defined

urng RandomGen

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

Return Value

Type: auto ref

A single random element drawn from the range. If it can, it will return a ref to the range element, otherwise it will return a copy.

Examples

auto rnd = MinstdRand0(42);

auto elem  = [1, 2, 3, 4, 5].choice(rnd);
version (D_LP64) // https://issues.dlang.org/show_bug.cgi?id=15147
assert(elem == 3);

Meta