Java Generator for Poisson and Uniform Distributions?

前端 未结 4 1164
野性不改
野性不改 2020-12-08 17:56

From what I understand, the standard generator is for the Normal Distribution. I have to generate random numbers according to the Normal, Uniform and Poisson Distributions,

4条回答
  •  眼角桃花
    2020-12-08 18:23

    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:

    1. A Poisson random variable is a sum of exponential random variables.
    2. 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.

提交回复
热议问题