Poisson distribution problem

本文介绍了一个使用Java实现的Poisson分布随机数生成器。该生成器通过给定的λ参数生成Poisson分布的数值。然而,当λ值大于745时,由于Math.exp(-λ)计算结果为0,导致程序陷入无限循环的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值