Skip to content

Instantly share code, notes, and snippets.

@chaance
Forked from motoishmz/Array.prototype.shuffle.js
Last active November 13, 2017 22:57
Show Gist options
  • Save chaance/df18a7fd1e660f0e0ea4e54823bfb895 to your computer and use it in GitHub Desktop.
Save chaance/df18a7fd1e660f0e0ea4e54823bfb895 to your computer and use it in GitHub Desktop.
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