Skip to content

Instantly share code, notes, and snippets.

@huozhi
Created July 14, 2017 02:54
Show Gist options
  • Save huozhi/e3ea6fc49049742456d50e519deeadcd to your computer and use it in GitHub Desktop.
Save huozhi/e3ea6fc49049742456d50e519deeadcd to your computer and use it in GitHub Desktop.
shuffle an array
// https://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array/6274381#6274381
/**
* Shuffles array in place. ES6 version
* @param {Array} a items The array containing the items.
*/
function shuffle(a) {
for (let i = a.length; i; i--) {
let j = Math.floor(Math.random() * i);
[a[i - 1], a[j]] = [a[j], a[i - 1]];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment