@pacote/shuffle
Version:
An implementation of the Durstenfeld algorithm for shuffling collections.
18 lines (15 loc) • 445 B
text/typescript
function random(min: number, max: number): number {
return min + Math.floor(Math.random() * (max - min))
}
function swap<T>(i: number, j: number, items: T[]): void {
const swapped = items[i]
items[i] = items[j]
items[j] = swapped
}
export function shuffle<T>(items: readonly T[]): T[] {
const shuffled = [...items]
for (let i = 0; i < items.length - 1; i++) {
swap(i, random(i, items.length), shuffled)
}
return shuffled
}