Last active
August 29, 2015 14:26
-
-
Save gorakhargosh/1cec5b09ef4f2839bb44 to your computer and use it in GitHub Desktop.
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
/** | |
* Fisher-Yates in-place shuffle. | |
* | |
* @see: http://bost.ocks.org/mike/algorithms/#shuffling | |
* @param {!Array} array An array of items. | |
*/ | |
var fisherYatesShuffle = function(array) { | |
var n = array.length | |
var t; | |
var i; | |
while (n) { | |
i = Math.random() * n-- | 0; // 0 ≤ i < n; also '| 0' truncates to int. | |
t = array[n]; | |
array[n] = array[i]; | |
array[i] = t; | |
} | |
return array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment