Skip to content

Instantly share code, notes, and snippets.

@nabrown
Created April 9, 2018 13:05
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 nabrown/ebff5bc272c965a3b24fe6f325af550b to your computer and use it in GitHub Desktop.
Save nabrown/ebff5bc272c965a3b24fe6f325af550b to your computer and use it in GitHub Desktop.
Another implementation of getRandosFromArray, using an efficient shuffle function
export const getRandosFromArray = function(arr, numRandos){
let arrClone = arr.slice(0)
let shuffled = shuffle(arrClone)
let randos = shuffled.slice(0, numRandos)
return randos
}
// https://bost.ocks.org/mike/shuffle/
const shuffle = function(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