UNPKG

fisher-yates

Version:

A compact module to randomly sort an Array

18 lines (12 loc) 286 B
module.exports = function shuffle (array, rng) { rng = rng || Math.random const result = [] for (let i = 0; i < array.length; ++i) { const j = Math.floor(rng() * (i + 1)) if (j !== i) { result[i] = result[j] } result[j] = array[i] } return result }