samedi 13 janvier 2018

Random distribution of integers with fixed total

I'm trying to get a random distribution of integers with total sum equal to N.

numbers = np.array(range(N))

for n in range(N):
    prob[n] = rd.randrange(10000)

sum = numpy.sum(prob)
prob = (prob / sum * n).astype(int)

The problem is that the result is not exactly equal to N because of float number approximation. For example N = 100 will yield a a total of about 95.

What is a pythonic solution to this problem?




Aucun commentaire:

Enregistrer un commentaire