Skip to content

Instantly share code, notes, and snippets.

@JeremyIglehart
Created June 17, 2016 20:25
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 JeremyIglehart/0303f987296d1509d93e92b19f02f86e to your computer and use it in GitHub Desktop.
Save JeremyIglehart/0303f987296d1509d93e92b19f02f86e to your computer and use it in GitHub Desktop.
A function which tests the distribution of _.sample()
// Option 1
(function(arraySize = 10, timesToRun = 1000){
let myArray = Array.apply(null, {length: arraySize}).map(Number.call, Number);
let resultsArray = Array.apply(null, Array(arraySize)).map(Number.prototype.valueOf,0);
for (let i = 0; i < timesToRun; i++) {
var result = _.sample(myArray);
resultsArray[result]++;
}
return resultsArray;
})(/*10, 1000*/);
// Option 2
(function(arraySize = 10, timesToRun = 1000){
let myArray = (new Array(arraySize)).map(Number.call, Number);
let resultsArray = (new Array(arraySize)).fill(0);
for (let i = 0; i < timesToRun; i++) {
var result = _.sample(myArray);
resultsArray[result]++;
}
return resultsArray;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment