The OpenD Programming Language

partialShuffle

Partially shuffles the elements of r such that upon returning r[0 .. n] is a random subset of r and is randomly ordered. r[n .. r.length] will contain the elements not in r[0 .. n]. These will be in an undefined order, but will not be random in the sense that their order after partialShuffle returns will not be independent of their order before partialShuffle was called.

r must be a random-access range with length. n must be less than or equal to r.length. If no RNG is specified, rndGen will be used.

  1. Range partialShuffle(Range r, size_t n, RandomGen gen)
  2. Range partialShuffle(Range r, size_t n)
    Range
    partialShuffle
    (
    Range
    )
    (
    Range r
    ,
    in size_t n
    )

Parameters

r Range

random-access range whose elements are to be shuffled

n size_t

number of elements of r to shuffle (counting from the beginning); must be less than r.length

Return Value

Type: Range

The shuffled random-access range.

Examples

auto rnd = MinstdRand0(42);

auto arr = [1, 2, 3, 4, 5, 6];
arr = arr.dup.partialShuffle(1, rnd);

version (D_LP64) // https://issues.dlang.org/show_bug.cgi?id=15147
assert(arr == [2, 1, 3, 4, 5, 6]); // 1<->2

arr = arr.dup.partialShuffle(2, rnd);
version (D_LP64) // https://issues.dlang.org/show_bug.cgi?id=15147
assert(arr == [1, 4, 3, 2, 5, 6]); // 1<->2, 2<->4

arr = arr.dup.partialShuffle(3, rnd);
version (D_LP64) // https://issues.dlang.org/show_bug.cgi?id=15147
assert(arr == [5, 4, 6, 2, 1, 3]); // 1<->5, 2<->4, 3<->6

Meta