Let me preface all this by the fact that none of this is truly random, I am talking about pseudo random number generators.
Let me also say that I have never had to do this for production quality code. I have done this for a hw assignment though, in Python. I simulated Poisson random variables.
The way that I did it made use of the following facts:
- A Poisson random variable is a sum of exponential random variables.
- We can use the inverse transform method to generate exponential random variables. http://en.wikipedia.org/wiki/Inverse_transform_sampling.
In particular, you can use the fact that: if X1, ..., Xn are independent standard exponential random variables, then Z = min(k : X1 + ... + Xk < λ) - 1 is Poisson(λ).
So, with that, I wrote the following code in python to generate Poisson values:
class Poisson:
"""Generate Poisson(lambda) values by using exponential
random variables."""
def __init__(self, lam):
self.__lam = lam
def nextPoisson(self):
sum = 0
n = -1
while sum < self.__lam:
n += 1
sum -= math.log(random.random())
return n
Example usage of the class is:
# Generates a random value that is Poisson(lambda = 5) distributed
poisson = Poisson(5)
poisson_value = poisson.nextPoisson
I posted this here because it is good to know that these kinds of relationships exist, and this inverse transform method gives you a general way to deal with generating random values following a particular continuous distribution.