I\'m either looking for an algorithm or a suggestion to improve my code to generate a list of random numbers that their sum equals some arbitrary number. With my code below
I ran into this problem and specifically needed integers. An answer is to use the multinomial.
import numpy.random, numpy
total_sum = 20
n = 6
v = numpy.random.multinomial(total_sum, numpy.ones(n)/n)
As the multinomial documentation explains, you have rolled a fair six-sided dice twenty times. v contains six numbers indicating the number of times each side of the dice came up. Naturally the elements of v have to sum to twenty. Here, six is n and twenty is total_sum.
With the multinomial, you can simulate an unfair dice as well, which is very useful in some cases.