Skip to content

shuffleArray

Jason Godesky edited this page Jul 11, 2026 · 1 revision

shuffleArray returns a new array with the same elements as the original array passed to it, but rearranged into a new, randomized order (using a Fisher–Yates shuffle). The array passed in is not mutated.

Parameters

arr: T[]

The array to shuffle.

Returns

A new array with all of the same elements as arr, but in a randomized order.

Examples

import { shuffleArray } from '@revolutionarygamesco/common'

shuffleArray<number>([1, 2, 3]) // Will return a new array with 1, 2, and 3, but in a randomized order

interface TestCase {
  n: number
  label: string
}

const cases: TestCase[] = [
  { n: 1, label: 'one' }
  { n: 2, label: 'two' },
  { n: 3, label: 'three' }
]

shuffleArray<TestCase>(cases) // Will return a new array with the three test cases, but in a randomized order

See also

Clone this wiki locally