Created
July 14, 2017 02:54
-
-
Save huozhi/e3ea6fc49049742456d50e519deeadcd to your computer and use it in GitHub Desktop.
shuffle an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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