Function shuffle

  • Category

    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

    • Optional mutate: true

      Optional, true if specified

    Returns void

    Example

    const array = [1, 2, 3];

    shuffle(array);

    See

    sample if you only want to select one element at random

    Array

  • Category

    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

    Example

    const array = [1, 2, 3];

    const shuffled = shuffle(array, false);

    See

    sample if you only want to select one element at random