Getting N random numbers whose sum is M

Short Answer:

Just generate N random numbers, compute their sum, divide each one by
the sum and multiply by M.

Longer Answer:

The above solution does not yield a uniform distribution which might be an issue depending on what these random numbers are used for.
Another method proposed by Matti Virkkunen:

Generate N-1 random numbers between 0 and 1, add the numbers 0 and 1
themselves to the list, sort them, and take the differences of
adjacent numbers.

This yields a uniform distribution as is explained here

Leave a Comment