For the code, see this GitHub link:
This code models a Poisson process. As you can see at the above link, we have a PoissonHeartbeat class that prints a “ping”-type statement on every event generated by the process. Let’s look at some of the more interesting lines of code.
self.events = events self.lambd = lambd
Explanation: events and lambd are instance variables, since every instance will have its own specific values for these variables. Note that lambd cannot be named lambda, since that is a reserved name in Python.
intervals = [random.expovariate(self.lambd) for i in range(self.numberOfEvents)]
We’re using a list expression to generate random time intervals drawn from the exponential distribution. We sample from that distribution using random.expovariate, which takes a lambda parameter and returns a random real-valued sample from the exponential distribution fully determined by lambda.
for v in intervals: time.sleep(v) print "ping! after interval ", v
We use Python’s
time.sleep to actually simulate the Poisson process based on the previously generated random time intervals.