title: percentage distribution - JavaScript Math.random()
body: <p>I have percentages <strong>10%</strong>, <strong>20%</strong> and <strong>70%</strong> to decide which action to take. I have two ways to decide: #1 let a new Math.random() decide every time or #2 keep one Math.random() result. See the code i run in the console (note the <code>rand1</code> and <code>rand2</code> methods):</p>
  
  <pre><code>console.log('chart1');
  var chart1;
  var rand1 = function() {
      if (Math.random() &lt;= 0.1) chart1[0]++;
      else if (Math.random() &lt;= 0.2) chart1[1]++;
      else chart1[2]++;
  };
  var runs = 10000;
  for (var j = 0; j &lt; 10; j++) {
      chart1 = [0,0,0];
      for (var i = 0; i &lt; runs; i++) rand1();
      var log = [];
      for (var k = 0; k &lt; chart1.length; k++) {
          log[k] = Math.round(chart1[k] / runs * 100);
      }
      console.log(log);
  }
  
  console.log('chart2');
  var chart2;
  var rand2 = function() {
      var rand = Math.random();
      if (rand &lt;= 0.1) chart2[0]++;
      else if (rand &gt;= 0.1 &amp;&amp; rand &lt;= 0.3) chart2[1]++;
      else chart2[2]++;
  };
  var runs = 10000;
  for (var j = 0; j &lt; 10; j++) {
      chart2 = [0,0,0];
      for (var i = 0; i &lt; runs; i++) rand2();
      var log = [];
      for (var k = 0; k &lt; chart2.length; k++) {
          log[k] = Math.round(chart2[k] / runs * 100);
      }
      console.log(log);
  }
  </code></pre>
  
  <p>it produces</p>
  
  <pre><code>chart1
  [10, 18, 72]
  [10, 17, 72]
  [11, 17, 72]
  [10, 18, 72]
  [10, 18, 72]
  [10, 18, 72]
  [10, 17, 73]
  [10, 18, 72]
  [10, 18, 72]
  [10, 18, 71]
  
  chart2 
  [9, 20, 71]
  [10, 21, 70]
  [10, 19, 71]
  [10, 20, 70]
  [10, 19, 71]
  [10, 20, 70]
  [10, 20, 70]
  [9, 20, 71]
  [10, 20, 71]
  [9, 20, 71] 
  </code></pre>
  
  <p>And i dont get why <code>chart1</code> tends to <code>..18, 72]</code> ?</p>