Skip to content

Instantly share code, notes, and snippets.

@Pushplaybang
Created April 27, 2015 12:21
Show Gist options
  • Save Pushplaybang/0aa1ec5645d67933b045 to your computer and use it in GitHub Desktop.
Save Pushplaybang/0aa1ec5645d67933b045 to your computer and use it in GitHub Desktop.
Fischer Yates Shuffle in Javascript
//as a function
function shuffleFischerYates(arr) {
var i = arr.length, j, temp;
while (--i > 0) {
j = Math.floor(Math.random()*(i+1));
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
// or extending a prototype
Array.prototype.shuffle = function() {
var = this.length, j, temp;
while (--i > 0) {
j = Math.floor(Math.random()*(i+1));
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment