Created
July 30, 2011 14:12
-
-
Save alexey-bass/1115567 to your computer and use it in GitHub Desktop.
JavaScript array shuffle
This file contains 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
/** | |
* @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