Skip to content

Instantly share code, notes, and snippets.

@webbower
Last active December 2, 2017 22:00
Show Gist options
  • Save webbower/2e90030641c6a3719c5df2bba17556c4 to your computer and use it in GitHub Desktop.
Save webbower/2e90030641c6a3719c5df2bba17556c4 to your computer and use it in GitHub Desktop.
Fisher-Yates shuffle implementation
// https://stackoverflow.com/a/2450976/2684520
// https://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