Skip to content

Instantly share code, notes, and snippets.

@alexey-bass
Created July 30, 2011 14:12
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 alexey-bass/1115567 to your computer and use it in GitHub Desktop.
Save alexey-bass/1115567 to your computer and use it in GitHub Desktop.
JavaScript array shuffle
/**
* @function
* @param {Boolean} $new Need a new array or shuffle this.
* @return {Array}
* @see http://www.hardcode.nl/subcategory_1/article_317-array-shuffle-function.htm
* @see http://yelotofu.com/2008/08/jquery-shuffle-plugin/
* @author Alexey Bass (albass)
*/
Array.prototype.shuffle = function($new) {
$new = $new || false;
var $a = !$new ? this : this.slice()
, $len = $a.length, $i = $len
, $p, $t;
while ($i--) {
$p = parseInt(Math.random() * $len);
$t = $a[$i], $a[$i] = $a[$p], $a[$p] = $t;
}
return $a;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment