Python - How to generate a random number with a bitwise probability? -


i need work out number of atoms decayed in specified time. given half life of radon-222 , have calculated probability 1 atom decay within 1 minute.

if have x atoms , probability y, how calculate number decay within 1 minute?

i have inefficient function @ moment (see below) can done more easily.

def decay(atomno, decayrate):     out = 0     atom in range(atomno):         if random.uniform(0,1)<decayrate:             out += 1     return out 

it sounds want random number binomial distribution, produced numpy.random.binomial.

for reasonably large number of atoms, can approximated normal distribution so:

from random import normalvariate  def binomialvariate(n, p):     """     generate random number binomial distribution       using normal-distribution approximation     """     return normalvariate(n * p, (n * p * (1 - p)) ** 0.5) 

which, used like

atoms = 1000000 decay_prob = 0.1  in range(10):     print(binomialvariate(atoms, decay_prob)) 

gives

100307.20222237767 99709.2126851899 99834.51936804672 100085.99501737293 100121.93115561221 100379.9532069239 99848.39057702095 99465.46179311829 100357.77320779095 99990.74240156381 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -