Skip to content

Instantly share code, notes, and snippets.

@AlexBezuska
Last active December 25, 2015 11:29
Show Gist options
  • Save AlexBezuska/6969089 to your computer and use it in GitHub Desktop.
Save AlexBezuska/6969089 to your computer and use it in GitHub Desktop.
Fisher Yates shuffle algorithm in JavaScript, taken from Pratik Deoghare on Stack Overflow. More info on the concept: http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
function fisherYates(myArray,nb_picks){
for (i = myArray.length-1; i > 1 ; i--){
var r = Math.floor(Math.random()*i);
var t = myArray[i];
myArray[i] = myArray[r];
myArray[r] = t;
}
return myArray.slice(0,nb_picks);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment