title: Generate N random numbers within a range with a constant sum
body: <p>I want to generate N random numbers drawn from a specif distribution (e.g uniform random) between [a,b] which sum to a constant C. I have tried a couple of solutions I could think of myself, and some proposed on similar threads but most of them either work for a limited form of problem or I can't prove the outcome still follows the desired distribution.</p>
  
  <p>What I have tried:
  Generage N random numbers, divide all of them by the sum of them and multiply by the desired constant. This seems to work but the result does not follow the rule that the numbers should be within [a:b].</p>
  
  <p>Generage N-1 random numbers add 0 and desired constant C and sort them. Then calculate the difference between each two consecutive nubmers and the differences are the result. This again sums to C but have the same problem of last method(the range can be bigger than [a:b].</p>
  
  <p>I also tried to generate random numbers and always keep track of min and max in a way that the desired sum and range are kept and come up with this code:</p>
  
  <pre><code>bool generate(function&lt;int(int,int)&gt; randomGenerator,int min,int max,int len,int sum,std::vector&lt;int&gt; &amp;output){
      /**
      * Not possible to produce such a sequence
      */
  if(min*len &gt; sum)
      return false;
  if(max*len &lt; sum)
      return false;
  
  int curSum = 0;
  int left = sum - curSum;
  int leftIndexes = len-1;
  int curMax = left - leftIndexes*min;
  int curMin = left - leftIndexes*max;
  
  for(int i=0;i&lt;len;i++){
      int num = randomGenerator((curMin&lt; min)?min:curMin,(curMax&gt;max)?max:curMax);
      output.push_back(num);
      curSum += num;
      left = sum - curSum;
      leftIndexes--;
      curMax = left - leftIndexes*min;
      curMin = left - leftIndexes*max;
  }
  
  return true;
  }
  </code></pre>
  
  <p>This seems to work but the results are sometimes very skewed and I don't think it's following the original distribution (e.g. uniform). E.g:</p>
  
  <pre><code>//10 numbers within [1:10] which sum to 50:
  generate(uniform,1,10,10,50,output);
  //result:
  2,7,2,5,2,10,5,8,4,5 =&gt; sum=50
  //This looks reasonable for uniform, but let's change to 
  //10 numbers within [1:25] which sum to 50:
  generate(uniform,1,25,10,50,output);
  //result:
  24,12,6,2,1,1,1,1,1,1 =&gt; sum= 50
  </code></pre>
  
  <p>Notice how many ones exist in the output. This might sound reasonable because the range is larger. But they really don't look like a uniform distribution.
  I am not sure even if it is possible to achieve what I want, maybe the constraints are making the problem not solvable. </p>