Site Logo

Shuffle

1 min read

Description

Shuffles an array in-place using the Fisher-Yates shuffle algorithm. This function randomly rearranges the elements of an array and returns the same array (mutated). It provides a uniform distribution, meaning each possible permutation has an equal probability of occurring.

Code

export const shuffle = <T>(arr: T[]): T[] => {
  let i = arr.length
  let j
  while (i) {
    j = Math.floor(Math.random() * i--)
    ;[arr[i], arr[j]] = [arr[j], arr[i]]
  }
  return arr
}
TS

Usage

const numbers = [1, 2, 3, 4, 5]
const shuffled = shuffle(numbers)
console.log(shuffled) [3, 1, 5, 2, 4]
console.log(numbers === shuffled) // true (same array reference)
TS
comment ondiscussions
cd ..
Shuffle • Shiyu's Hideout