Skip to content

Instantly share code, notes, and snippets.

@blackmjck
Created February 12, 2014 15:51
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save blackmjck/8958066 to your computer and use it in GitHub Desktop.
Array shuffle method using Mike Bostock's Fisher-Yates Shuffle implementation. Original is here: http://bost.ocks.org/mike/shuffle/
/**
* Extends the primitive Array with a random sort function
*
* @returns {Array}
*/
;Array.prototype.shuffle = function() {
var arr = this,
m = arr.length,
t,
i;
// While there remain elements to shuffle…
while ( m ) {
// Pick a remaining element…
i = Math.floor( Math.random() * m-- );
// And swap it with the current element.
t = arr[ m ];
arr[ m ] = arr[ i ];
arr[ i ] = t;
}
return arr;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment