Skip to content

Instantly share code, notes, and snippets.

@es
Last active December 27, 2015 06:49
Show Gist options
  • Save es/7283839 to your computer and use it in GitHub Desktop.
Save es/7283839 to your computer and use it in GitHub Desktop.
In place Fisher-Yates shuffle in Javascript.
function FisherYatesShuffle (arr) {
var randInt = function (min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
};
var swap = function (pos1, pos2) {
var temp = arr [pos1];
arr [pos1] = arr [pos2];
arr [pos2] = temp;
};
for (var i = arr.length - 1; i != 0; i--) {
swap (randInt (0, i), i);
}
return arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment