Created
April 20, 2015 15:39
-
-
Save billmei/8a8544817225a3927ee1 to your computer and use it in GitHub Desktop.
Shuffle algorithm for JavaScript
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
// 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