Skip to content

Instantly share code, notes, and snippets.

@billmei
Created April 20, 2015 15:39
Show Gist options
  • Save billmei/8a8544817225a3927ee1 to your computer and use it in GitHub Desktop.
Save billmei/8a8544817225a3927ee1 to your computer and use it in GitHub Desktop.
Shuffle algorithm for JavaScript
// Randomly shuffles an array in-place using Fisher-Yates algorithm
// http://stackoverflow.com/a/12646864
Array.prototype.shuffle = function() {
for (var i = this.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = this[i];
this[i] = this[j];
this[j] = temp;
}
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment