<p>To generate N positive numbers that sum to a positive number M at random, where each possible combination is equally likely:</p>
<ul>
<li><p>Generate N exponentially-distributed random variates. One way to generate such a number can be written as—</p>
<pre><code>  number = -ln(1.0 - RNDU())
</code></pre>
<p>where <code>ln(x)</code> is the natural logarithm of <code>x</code> and <code>RNDU()</code> is a method that returns a uniform random variate greater than 0 and less than 1.  Note that generating the N variates with a uniform distribution is not ideal because a biased distribution of random variate combinations will result.  However, the implementation given above has several problems, such as <a href="https://arxiv.org/abs/1704.07949" rel="nofollow noreferrer">being ill-conditioned at large values</a> because of the distribution's right-sided tail, especially when the implementation involves floating-point arithmetic.  Another implementation is given in <a href="https://stackoverflow.com/questions/2106503/pseudorandom-number-generator-exponential-distribution/65395376#65395376">another answer</a>.</p>
</li>
<li><p>Divide the numbers generated this way by their sum.</p>
</li>
<li><p>Multiply each number by M.</p>
</li>
</ul>
<p>The result is N numbers whose sum is approximately equal to M (I say &quot;approximately&quot; because of rounding error). See also the Wikipedia article <a href="https://en.wikipedia.org/wiki/Dirichlet_distribution" rel="nofollow noreferrer">Dirichlet distribution</a>.</p>
<p>This problem is also equivalent to the problem of <a href="https://stackoverflow.com/questions/3010837/sample-uniformly-at-random-from-an-n-dimensional-unit-simplex">generating random variates uniformly from an N-dimensional unit simplex</a>.</p>
<hr />
<p>However, for better accuracy (compared to the alternative of using floating-point numbers, which often occurs in practice), you should consider generating <a href="https://peteroupc.github.io/randomfunc.html#Random_Integers_with_a_Given_Positive_Sum" rel="nofollow noreferrer"><code>n</code> random <em>integers</em> that sum to an <em>integer</em> <code>m * x</code></a>, and treating those integers as the numerators to <code>n</code> rational numbers with denominator <code>x</code> (and will thus sum to <code>m</code> assuming <code>m</code> is an integer). You can choose <code>x</code> to be a large number such as 2<sup>32</sup> or 2<sup>64</sup> or some other number with the desired precision. If <code>x</code> is 1 and <code>m</code> is an integer, this solves the problem of generating random <em>integers</em> that sum to <code>m</code>.</p>
<p>The following pseudocode shows two methods for generating <code>n</code> uniform random integers with a given positive sum, in random order. (The algorithm for this was presented in Smith and Tromble, &quot;Sampling Uniformly from the Unit Simplex&quot;, 2004.) In the pseudocode below—</p>
<ul>
<li>the method <code>PositiveIntegersWithSum</code> returns <code>n</code> integers <strong>greater than 0</strong> that sum to <code>m</code>, in random order,</li>
<li>the method <code>IntegersWithSum</code> returns <code>n</code> integers <strong>0 or greater</strong> that sum to <code>m</code>, in random order, and</li>
<li><code>Sort(list)</code> sorts the items in <code>list</code> in ascending order (note that sort algorithms are outside the scope of this answer).</li>
</ul>
<p> </p>
<pre><code>METHOD PositiveIntegersWithSum(n, m)
    if n &lt;= 0 or m &lt;=0: return error
    ls = [0]
    ret = NewList()
    while size(ls) &lt; n
      c = RNDINTEXCRANGE(1, m)
      found = false
      for j in 1...size(ls)
        if ls[j] == c
          found = true
          break
        end
      end
      if found == false: AddItem(ls, c)
    end
    Sort(ls)
    AddItem(ls, m)
    for i in 1...size(ls): AddItem(ret,
        ls[i] - ls[i - 1])
    return ret
END METHOD

METHOD IntegersWithSum(n, m)
  if n &lt;= 0 or m &lt;=0: return error
  ret = PositiveIntegersWithSum(n, m + n)
  for i in 0...size(ret): ret[i] = ret[i] - 1
  return ret
END METHOD
</code></pre>
<p>Here, <code>RNDINTEXCRANGE(a, b)</code> returns a uniform random integer in the interval [a, b).</p>
Getting N random numbers whose sum is M
<p>I want to get N random numbers whose sum is a value.</p>

<p>For example, let's suppose I want 5 random numbers that sum to 1.</p>

<p>Then, a valid possibility is:</p>

<pre><code>0.2 0.2 0.2 0.2 0.2
</code></pre>

<p>Another possibility is:</p>

<pre><code>0.8 0.1 0.03 0.03 0.04
</code></pre>

<p>And so on. I need this for the creation of a matrix of belongings for Fuzzy C-means.</p>