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
[EDIT to add context]
What I am really doing is n random walks in a kind of Markov chain as follow:
def rand_walk(n,state):
next_states,prob = complicated_function(state) // compute the next states and their probability
succ = distribute_over_next_states(n, prob) // compute how many walk goes to which states
accu = complicated_function_2(state) // accumulator for the result
for ns in range(0,len(next_states)):
accu += rand_walk(succ[i],next_states[I])
return accu
The point is that the computation of the next states and their probability is costly thus I avoid computing it to many times (thus I avoid doing n runs sequential). That is why I want to distribute the n following the given probability.
I hope this is somehow clear enough to understand ...