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)
  2. auto ref choice(Range range)
    ref
    choice
    (
    Range
    )
    (
    auto ref Range range
    )

Parameters

range Range

a random access range that has the length property defined

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 (X86_64) // https://issues.dlang.org/show_bug.cgi?id=15147
assert(elem == 3);

Meta

Suggestion Box / Bug Report