Skip to content

Instantly share code, notes, and snippets.

@keeganwatkins
Created November 17, 2010 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keeganwatkins/704103 to your computer and use it in GitHub Desktop.
Save keeganwatkins/704103 to your computer and use it in GitHub Desktop.
Simple mechanism for randomizing the contents of an Array
var shuffle = function(source) {
var ret = Array.prototype.slice.call(source),
len = source.length,
i = len,
rand, temp;
while (i--) {
rand = parseInt( Math.random() * len, 10 );
temp = ret[ i ];
ret[ i ] = ret[ rand ];
ret[ rand ] = temp;
}
return ret;
};
// Usage:
shuffle( ['a', 'b', 'c', 'd'] )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment