In my python code there are a few places where I have a population where there is x chance of something happening to each individual, but I just need the amount of people affected.
amount = 0
population = 500
chance = 0.05
for p in range(population):
if random.random() < chance:
amount += 1
My gut tells me there must be a less brute force way to do this than calling random.random() 500 times. Some math or stats terminology or function that I don't know.
amount = population * chance * random.random()
is too variable for what I need.
The distribution of the sum of n 0-1 random variables, each with probability p is called a binomial distribution with parameters n and p. I believe numpy.random.binomial will do what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With