jeudi 5 juillet 2018

Genrating random (int) list of given sum python

I want to simulate n random choices with given probabilities prob.

My current solution is the following:

from random import choices

result = [0]*len(prob)
population = list(range(0,len(prob)))
ch = choices(population, weights=prob, k=n)
for i in ch:
   result[i] += 1

My problem is that I call this code a large number of times and usually with large n and this solution does not seem efficient at all.

Is there a better way of doing it (like a pre-build function of some library)?

To sumarize I want the most efficient way of building a random list summing to $n$ such that the probability of obtaining a given list is equal to the probability of obtaining this list as n random choices with probability prob.

Thank you




Aucun commentaire:

Enregistrer un commentaire