Array
shuffle

🔀 Randomizes the order of elements in an array using the Fisher–Yates shuffle (opens in a new tab) algorithm.

Immutable: This function does not mutate the original array.

Syntax

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

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)

Related

Try