Skip to content

Instantly share code, notes, and snippets.

@shaundillon
Created March 23, 2015 14:21
Show Gist options
  • Save shaundillon/b2414c9eef3b92f3910b to your computer and use it in GitHub Desktop.
Save shaundillon/b2414c9eef3b92f3910b to your computer and use it in GitHub Desktop.
Fisher-Yates JS implementation
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