python - Trouble with calculating random occurance -


let's have code

import random  def foofunc():     return 1 

what overall chance of foofunc being executed when using code below?

if random.randrange(4096)==1:     foofunc() if random.randrange(256)==1:     foofunc() 

each call random.randrange can treated independent random selection, provided don't know seed , happy treat output of prng random variable.

what's overall chance of foofunc being executed?

assuming don't care tracking whether foofunc called twice? normal probability calculation, similar "what chance of rolling @ least 1 6 when roll 2 dice". this, easier re-formulate question "what probability don't roll 6", , subtract 1.0, because there 1 combination of failing both checks, whilst there 3 combinations of succeeding 1 or other or both.

so p = 1 - ((4095/4096) * (255/256))


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -