Forked from motoishmz/Array.prototype.shuffle.js
Last active
November 13, 2017 22:57
-
-
Save chaance/df18a7fd1e660f0e0ea4e54823bfb895 to your computer and use it in GitHub Desktop.
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
Array.prototype.shuffle = function() { | |
var i = this.length, // array length to determine current index in loop | |
r, // random index value | |
p; // current index value | |
// Loop through array indicies | |
while ( i-- ) { | |
// always returns less than the index to ensure it always exists | |
r = Math.floor( Math.random() * i ); | |
// set current value var | |
p = this[i]; | |
// Swap current index value with the random index value | |
this[i] = this[r]; | |
this[r] = p; | |
} | |
return this; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment