-2

The setup:

I am using _.sample() for my first time in some client's code and I wanted to test it to make sure that it produced an even distribution of samples time after time.

To test this, I created the following code:

(function(arraySize, timesToRun){
  arraySize = arraySize || 10;
  timesToRun = 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);

This runs perfectly in my Chrome browser to give me the following result:

[93, 112, 97, 87, 97, 107, 97, 104, 105, 101]

As expected, with other inputs (20, 10000) we get:

[646, 663, 641, 749, 648, 686, 642, 631, 663, 688, 691, 639, 672, 663, 678]

So... That's it, the code does what it needs for me, completely acceptable.


Here is what I'm looking for:

Explanation for the following:

An answer will be accepted if it can answer any/all of these three questions.

  1. I do not understand how Array.apply(null, {length: arraySize}).map(Number.call, Number); is working. Specifically the part that is VERY confusing to me is what is happening in the map call: map(Number.call, Number) - Why does this produce an array that looks like [0,1,2,3,4,5,6,7,8,9]? Seeing as this question is getting sliced up into separate questions, I've asked this specific question elsewhere
  2. Why do we need Number.prototype.valueOf in map(Number.prototype.valueOf,0) I farmed this question out to a new post this has been answered here
  3. Is there a better way to set default values to arraySize and timesToRun? (Note: I'm putting the same value in the function call just for convince - I know this is not required.)

Please make any other critiques to the code, I'm asking this question to learn more about Javascript and to see if there are any obvious improvements which could be made to this function.


Wish List:

Perhaps anyone could comment if they know a good way to do this:

I'd like to find a neat clean way to create graphical output for this test. Does anyone know an easy library to use so that I can graph this. Maybe for more input we could get a nice bell curve? - Here is a simple way - I'm putting this here inline because again, this question has been downvoted, but I wanted to give the information for anyone else looking to do this.


Important Note!

This question was formatted improperly. I will split this question up into smaller questions and post them separately. I've made this edit after reading: https://blog.stackoverflow.com/2010/09/good-subjective-bad-subjective/

... Well, I can't close this question - so I'll leave the original post below. If anyone decides to answer this, I'm still looking for the answer to these questions and I will accept.

Community
  • 1
  • 1
Jeremy Iglehart
  • 4,281
  • 5
  • 25
  • 38
  • [@robertklep](http://stackoverflow.com/users/893780/robertklep) yes, I created the code. The code I found from [this SO question](http://stackoverflow.com/questions/3746725/create-a-javascript-array-containing-1-n) was very helpful and seamed like the best approach. But I don't understand how `(Number.Call, Number)` is actually doing what it does. I understand **what** it does, but I don't understand **why**, or **how** it does it. I thought that asking questions was okay on StackOverflow - That's why I came here, to learn why works this way. I couldn't find the answer through other means – Jeremy Iglehart Jun 17 '16 at 17:33
  • [@MikeMcCaughan](http://stackoverflow.com/users/215552/mike-mccaughan) I did do my own research. I'm not stuck with anything - I was just looking for some extra deeper explanation from people one StackOverflow. I see people recommending libraries, and methods to achieve a goal all the time on SO, thought it was appropriate to specifically ask for this. Also, other people can learn from answers provided. Would you suggest a better way to ask this question? Maybe breaking it up into several smaller questions? According to your profile you're #SOreadytohelp - please add constructive comments – Jeremy Iglehart Jun 17 '16 at 17:36
  • Did you read the links I put into my comment? The former details many ways to improve questions, and the latter details why library recommendation questions are off-topic. – Heretic Monkey Jun 17 '16 at 17:40
  • @MikeMcCaughan [help center](http://stackoverflow.com/help/dont-ask) pointed me over to [Good Subjective Bad Subjective](http://blog.stackoverflow.com/2010/09/good-subjective-bad-subjective/) After reading that page, I think I see your point. I'll remove my question. – Jeremy Iglehart Jun 17 '16 at 17:47

1 Answers1

0

Due to the complaints that this question was not very direct I created other posts to get the answers I was looking for. This answers the main questions I had.

Answers:

  1. Understanding how array.prototype.call() works explains the output of this pattern. A full detailed answer can be found here: Mapping Array in Javascript with sequential numbers
  2. The reason why is because of how array.prototype.map() works - full answer here: Why do we use Number.prototype.valueOf inside of a map() function
  3. Yes, as of ES6 there is an alternate way to set a default for an argument in javascript:

(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]++;
  }
  // Turned into console.log() for StackOverflow Snippet
  // return resultsArray;
  console.log(resultsArray);
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

And here is a simple way to set this up graphically if anyone is interested.

Community
  • 1
  • 1
Jeremy Iglehart
  • 4,281
  • 5
  • 25
  • 38