Skip to content

Instantly share code, notes, and snippets.

@teckliew
Last active October 6, 2015 22:45
Show Gist options
  • Save teckliew/2b24157967cd3c7feb06 to your computer and use it in GitHub Desktop.
Save teckliew/2b24157967cd3c7feb06 to your computer and use it in GitHub Desktop.
Non-bias random array shuffle. For more about the Fisher–Yates shuffle, see the Wikipedia article and Jeff Atwood’s post, “The Danger of Naïveté” (2007). Sauce: http://bost.ocks.org/mike/shuffle/
function shuffle(array) {
var m = array.length, t, i;
// While there remain elements to shuffle…
while (m) {
// Pick a remaining element…
i = Math.floor(Math.random() * m--);
// And swap it with the current element.
t = array[m];
array[m] = 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