View source code
Display the source code in std/random.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

std.random.RandomCover/randomCover - multiple declarations

Function randomCover

Covers a given range r in a random manner, i.e. goes through each element of r once and only once, just in a random order. r must be a random-access range with length.

auto auto randomCover(Range, UniformRNG) (
  Range r,
  auto ref UniformRNG rng
)
if (isRandomAccessRange!Range && isUniformRNG!UniformRNG);

auto auto randomCover(Range) (
  Range r
)
if (isRandomAccessRange!Range);

If no random number generator is passed to randomCover, the thread-global RNG rndGen will be used internally.

Parameters

NameDescription
r random-access range to cover
rng (optional) random number generator to use; if not specified, defaults to rndGen

Returns

Range whose elements consist of the elements of r, in random order. Will be a forward range if both r and rng are forward ranges, an input range otherwise.

Example

import std.algorithm.comparison : equal;
import std.range : iota;
auto rnd = MinstdRand0(42);

version (X86_64) // https://issues.dlang.org/show_bug.cgi?id=15147
assert(10.iota.randomCover(rnd).equal([7, 4, 2, 0, 1, 6, 8, 3, 9, 5]));

Struct RandomCover

Covers a given range r in a random manner, i.e. goes through each element of r once and only once, just in a random order. r must be a random-access range with length.

struct RandomCover(Range, UniformRNG)
  
if (isRandomAccessRange!Range && (isUniformRNG!UniformRNG || is(UniformRNG == void)));

If no random number generator is passed to randomCover, the thread-global RNG rndGen will be used internally.

Parameters

NameDescription
r random-access range to cover
rng (optional) random number generator to use; if not specified, defaults to rndGen

Returns

Range whose elements consist of the elements of r, in random order. Will be a forward range if both r and rng are forward ranges, an input range otherwise.

Example

import std.algorithm.comparison : equal;
import std.range : iota;
auto rnd = MinstdRand0(42);

version (X86_64) // https://issues.dlang.org/show_bug.cgi?id=15147
assert(10.iota.randomCover(rnd).equal([7, 4, 2, 0, 1, 6, 8, 3, 9, 5]));

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)

License

Boost License 1.0.