How do I generate discrete random events with a Poisson distribution?

前端 未结 2 1302
醉酒成梦
醉酒成梦 2021-02-15 18:12

I\'m aware of Knuth\'s algorithm for generating random Poisson distributed numbers (below in Java) but how do I translate that into calling a method, generateEvent()

2条回答
  •  甜味超标
    2021-02-15 18:24

    If you are looking to simulate the inter-event arrival time, you want the exponential distribution.

    Take a look at Pseudorandom Number Generator - Exponential Distribution

    Your code would then look like this:

    // Note L == 1 / lambda
    public double poissonRandomInterarrivalDelay(double L) {
        return (Math.log(1.0-Math.random())/-L;
    }
    

    ...

    while (true){
        // Note -- lambda is 5 seconds, convert to milleseconds
        long interval= (long)poissonRandomInterarrivalDelay(5.0*1000.0);
        try {
            Thread.sleep(interval);
            fireEvent();
    }
    

提交回复
热议问题