title: How to generate random numbers that add up to a certain number and are generated in a range in JavaScript?
body: <p>I am trying to make this thing where it generates 7 random numbers. I am using</p>
  
  <pre><code>function getRandomInt(min, max) {
      return Math.floor(Math.random() * (max - min + 1)) + min;
  }
  
  function generateNum(max, thecount) {
      var r = [];
      var currsum = 0;
      for (var i = 0; i &lt; thecount - 1; i++) {
          r[i] = getRandomInt(15, max - (thecount - i - 1) - currsum);
          currsum += r[i];
      }
      r[thecount - 1] = max - currsum;
  
  
      return r;
  
  }
  </code></pre>
  
  <p>This will sometimes return numbers that are <code>NaN</code> or are greater than 40 (which needs to be the max)</p>
  
  <p>or less than 15 (which needs to be the min) and even less than 0.</p>
  
  <p>It generates numbers that add up to another random number which is somewhere between 110 or 150.</p>
  
  <p>How can I make it add up to the total random number and still be in a certain range?</p>