🔀 Randomizes the order of elements in an array using the Fisher–Yates shuffle algorithm.

Info

Immutable: This function does not mutate the original array.

Syntax

TypeScript
import { shuffle } from '@opentf/std';

shuffle<T>(arr: T[]): T[]

Parameters

  • arr: The source array to shuffle.

Returns

A new array with the elements in randomized order.

Examples

TypeScript
shuffle([]) //=> []

shuffle([1]) //=> [1]

shuffle([1, 2, 3, 4, 5]) //=> [2, 4, 5, 1, 3] (random result)

shuffle('Apple') //=> ['p', 'e', 'A', 'l', 'p'] (random result)
Last updated on
Edit this page