Function shuffle

  • Experimental

    Shuffles the provided array, returning a copy and without modifying the original array.

    UNSTABLE: New API, yet to be vetted.

    Type Parameters

    • T

      The type of the items in the array

    Parameters

    • items: readonly T[]

      The items to shuffle

    • Optionaloptions: RandomOptions

      The options for the random number generator

    Returns T[]

    A shuffled copy of the provided items

    import { shuffle } from "@std/random";

    const items = [1, 2, 3, 4, 5];

    shuffle(items); // [2, 5, 1, 4, 3]
    shuffle(items); // [3, 4, 5, 1, 2]
    shuffle(items); // [5, 2, 4, 3, 1]

    items; // [1, 2, 3, 4, 5] (original array is unchanged)