Skip to content

Instantly share code, notes, and snippets.

@orian
Created August 22, 2013 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orian/6304550 to your computer and use it in GitHub Desktop.
Save orian/6304550 to your computer and use it in GitHub Desktop.
Let's say that you have an event which happen at each second with probability p, each second is independent on previous and next one. Instead of tossing a coin every second, one can try to toss a coin once and estimate when a next event is going to happen, and then call the function again at a moment of event to get next event time and so on.
#!/usr/bin/python
import random
class BernoulliPbb(object):
def __init__(self, p):
self.p = p
def Estimate(self):
r = random.random()
i = 0
pbb = self.p
pbb_sum = self.p
while r >= pbb_sum:
pbb *= self.p
pbb_sum += pbb
print pbb, pbb_sum
i += 1
return i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment