Skip to main content

shuffle

shuffle() function

Returns a new array with the elements of the input array shuffled randomly using Fisher-Yates algorithm.

Signature:

declare function shuffle<T extends Iterable<unknown> | AsyncIterable<unknown>>(iterable: T, seed?: number): ReturnArrayType<T>;

Example

shuffle([1, 2, 3, 4, 5]); // [3, 1, 5, 2, 4] (random order)
shuffle("hello"); // ["o", "e", "l", "h", "l"] (random order) Only ASCII codes are shuffled in the case of a string.
shuffle([1, 2, 3, 4, 5], 42); // [2, 4, 1, 5, 3] (deterministic with seed 42)

Open Source Code