shuffle

import { shuffle } from "jsr:@ende/random";

function ยท experimental

function shuffle<T>(items: readonly T[], options?: RandomOptions): T[]

Shuffles the provided array, returning a copy and without modifying the original array.

items: readonly T[]

The items to shuffle

options?: RandomOptions

The options for the random number generator

Returns

A shuffled copy of the provided items

Example: Usage

import { shuffle } from "@ende/random";

const items = [1, 2, 3, 4, 5];

shuffle(items); // [2, 5, 1, 4, 3]
shuffle(items); // [3, 4, 5, 1, 2]
shuffle(items); // [5, 2, 4, 3, 1]

items; // [1, 2, 3, 4, 5] (original array is unchanged)

Source