shuffle

Shuffles the elements of array in-place.

  1. T[] shuffle(T[] array, double rand)
  2. T[] shuffle(T[] array, size_t delegate(size_t i) new_index)
    T[]
    shuffle
    (
    T
    )
    (
    T[] array
    ,
    scope size_t delegate
    (
    size_t i
    )
    new_index
    )

Parameters

array T[]

array with elements to shuffle

new_index size_t delegate
(
size_t i
)

returns the new index for the array element whose index is currently i. i is guaranteed to be in the range [1 .. array.length - 1]; the returned index should be in the range [0 .. i] and must be in range [0 .. array.length - 1].

Return Value

Type: T[]

shuffled array

Examples

int[] arr = [ 1, 2, 3, 4 ];
int[] orig = arr.dup;
auto modified = shuffle(arr, (size_t i) { return i; });
test!("==")(modified, orig);

Meta