Poisson distribution problem

I need to generate a set of numbers with Poisson distribution, given a lambda. I got a piece of code from Internet as follows:

 

----------------------------------------------------------------------

 

public class Poisson {
   
        /** e^(-lambda) */
    private double elambda;
    private Random rand;
   
        /** Creates a variable with a given mean. */
    public Poisson(double lambda) {
        elambda = Math.exp(-lambda);
        rand = new Random();
        //System.out.printf("%nThe rand is: ", rand);
    }
   
    public int next() {
        double product = 1;
        int count =  0;
        int result=0;
        while(product >= elambda) {
            product *= rand.nextDouble();
            result = count;
            count++; // keep result one behind
        }
        return result;
    }

}

 

----------------------------------------------------------------------

 

It indeed works. But when I increase lambda to 746, or higher, it will go to an endless loop. 745 or less is ok. Then I found that Math.exp(-746) = 0.

 

So if I am looking for a distribution with higher lambda than 745, I cannot use this code. I still need to find a way, if my simulation program really need such number.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章