如何用泊松分布产生离散随机事件? [英] How do I generate discrete random events with a Poisson distribution?
本文介绍了如何用泊松分布产生离散随机事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
int poissonRandomNumber(int lambda){
double L = Math.exp(-lambda);
int k = 0;
double p = 1;
do {
k = k + 1;
double u = Math.random();
p = p * u;
} while(p> L);
return k - 1;
}
解决方案
如果你正在模拟事件到达时间,你想要指数分布。
您的代码将如下所示:
//注意L == 1 / lambda
public double poissonRandomInterarrivalDelay(double L){
return(Math.log(1.0- Math.random())/ - L;
}
...
while(true){
//注 - lambda为5秒,转换为milleseconds
long interval = (long)poissonRandomInterarrivalDelay(5.0 * 1000.0);
try {
Thread.sleep(interval);
fireEvent();
}
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()
, randomly over time?
int poissonRandomNumber(int lambda) {
double L = Math.exp(-lambda);
int k = 0;
double p = 1;
do {
k = k + 1;
double u = Math.random();
p = p * u;
} while (p > L);
return k - 1;
}
解决方案
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();
}
这篇关于如何用泊松分布产生离散随机事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文