Created
March 23, 2015 14:21
-
-
Save shaundillon/b2414c9eef3b92f3910b to your computer and use it in GitHub Desktop.
Fisher-Yates JS implementation
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
function shuffle(array) { | |
var unshuffled = array.length, | |
copy, | |
remaining; | |
// While there are unshuffled elements | |
while (unshuffled) { | |
// Get one that hasn't been replaced yet | |
remaining = Math.floor(Math.random() * unshuffled--); | |
// And replace it with the unshuffled element | |
copy = array[unshuffled]; | |
array[unshuffled] = array[remaining]; | |
array[remaining] = copy; | |
} | |
return array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment