Skip to content

Instantly share code, notes, and snippets.

@gfcarvalho
Created April 1, 2014 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gfcarvalho/9907168 to your computer and use it in GitHub Desktop.
Save gfcarvalho/9907168 to your computer and use it in GitHub Desktop.
Shuffles a given array using an implemented version of Fisher–Yates algorithm
function shuffle(array) {
var counter = array.length, temp, index;
// While there are elements in the array
while (counter--) {
// Pick a random index
index = (Math.random() * (counter+1)) | 0;
// And swap the last element with it
temp = array[counter];
array[counter] = array[index];
array[index] = temp;
}
return array;
}
@gfcarvalho
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment