Function shuffle

  • A mutating (in-place) uniformly random array shuffle.

    Time complexity: O(n)

    Space complexity: O(1)

    Type Parameters

    • T

    Parameters

    • array: T[]

      Array to shuffle

    • Optionalmutate: true

      Optional, true if specified

    Returns void

    const array = [1, 2, 3];

    shuffle(array);

    sample if you only want to select one element at random

    Array

  • A non-mutating uniformly random array shuffle.

    Time complexity: O(n)

    Space complexity: O(n)

    Type Parameters

    • T

    Parameters

    • array: ArrayLike<T> & Iterable<T>

      Array to shuffle

    • mutate: false

      false

    Returns T[]

    The shuffled array

    Array

    const array = [1, 2, 3];

    const shuffled = shuffle(array, false);

    sample if you only want to select one element at random