I would like to create a function in Java that generates Poisson arrivals given the mean arrival rate (lambda) and the mean service rate (mu).
In my example I have: 2,2 requests/day, in other words 2,2 arrivals/day and a mean service time of 108 hours. Considering that my program starts at t=0 minutes, I would like to create a function that returns arrivals[], which will contain, t1, t2, and a possible t3. T1,t2 and t3 are the instants (in minutes) during the day where this arrivals occur. I have the following restrictions:
t1 < t2 < t3 < 1440 minutes (24 hours*60 minutes/hour)
t2-t1 > 108 minutes
t3-t2 > 108 minutes
t3+ 108 minutes < 1440 minutes
Can someone please help me?
Thank you,
Ana
You can use this algorithm proposed by D. Knuth:
private static int getPoissonRandom(double mean) {
Random r = new Random();
double L = Math.exp(-mean);
int k = 0;
double p = 1.0;
do {
p = p * r.nextDouble();
k++;
} while (p > L);
return k - 1;
}
To understand how this works note that after k iterations the loop condition becomes
p1 * p2 * ... * pk > L
which is equivalent to
-ln(p1)/mean -ln(p2)/mean ... -ln(pk)/mean > 1
Note that if p is uniformly distributed then -ln(p)/mean has exponential distribution with a given mean. A random variable with Poisson distribution is equal to the number of times a given event occurs within a fixed interval when the lengths of the intervals between events are independent random variables with exponential distribution. Since we use the mean of the Poisson distribution as the mean of the exponential distribution for the intervals between events, the fixed internal in which we count occurrences is unit length. Thus, the loop condition sums up the lengths of the intervals between events and checks whether we have gone beyond the unit interval. If we have gone beyond the unit interval while counting kth event, then k-1 events occurred within the interval and so we return k-1.