GuobaGuoba Utils

shuffle

@guoba-ai/utils / array / shuffle

Function: shuffle()

function shuffle<T>(array): T[];

Defined in: array.ts:132

Randomly shuffle an array. Returns a new array.

Parameters

array

T[]

The array to shuffle

Returns

T[]

A new shuffled array

Example

shuffle([1, 2, 3, 4, 5]) // [3, 1, 5, 2, 4] (random order)

const input = [1, 2, 3]
shuffle(input) === input // false

shuffle([1]) // [1]

Warning

Uses Math.random(), so it is not suitable for cryptographic or security-sensitive randomness.

On this page