Skip to content

Instantly share code, notes, and snippets.

@rigwild
Forked from nikolas/array-shuffle.js
Last active August 22, 2019 15:04
Show Gist options
  • Save rigwild/3c8459410ffd569b3e7b734d1642a436 to your computer and use it in GitHub Desktop.
Save rigwild/3c8459410ffd569b3e7b734d1642a436 to your computer and use it in GitHub Desktop.
/**
* Shuffle an array without changing the original one
* @param {any[]} array Any array
* @returns {any[]} Randomized array
*/
const shuffleArray = array => {
const a = array.slice()
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1))
[a[i], a[j]] = [a[j], a[i]]
}
return a
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment