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.

Function std.random.randomShuffle

Shuffles elements of r using gen as a shuffler. r must be a random-access range with length. If no RNG is specified, rndGen will be used.

Range randomShuffle(Range, RandomGen) (
  Range r,
  ref RandomGen gen
)
if (isRandomAccessRange!Range && isUniformRNG!RandomGen);

Range randomShuffle(Range) (
  Range r
)
if (isRandomAccessRange!Range);

Parameters

NameDescription
r random-access range whose elements are to be shuffled
gen (optional) random number generator to use; if not specified, defaults to rndGen

Returns

The shuffled random-access range.

Example

auto rnd = MinstdRand0(42);

auto arr = [1, 2, 3, 4, 5].randomShuffle(rnd);
version (X86_64) // Issue 15147
writeln(arr); // [3, 5, 2, 4, 1]

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.