UNPKG

@pacote/shuffle

Version:

An implementation of the Durstenfeld algorithm for shuffling collections.

16 lines 427 B
function random(min, max) { return min + Math.floor(Math.random() * (max - min)); } function swap(i, j, items) { const swapped = items[i]; items[i] = items[j]; items[j] = swapped; } export function shuffle(items) { const shuffled = [...items]; for (let i = 0; i < items.length - 1; i++) { swap(i, random(i, items.length), shuffled); } return shuffled; } //# sourceMappingURL=index.js.map