Skip to content

Instantly share code, notes, and snippets.

@gorakhargosh
Last active August 29, 2015 14:26
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 gorakhargosh/1cec5b09ef4f2839bb44 to your computer and use it in GitHub Desktop.
Save gorakhargosh/1cec5b09ef4f2839bb44 to your computer and use it in GitHub Desktop.
/**
* 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