已编写了一种方法,该方法以分钟为单位递增时间,该方法模拟处理超市队列中物品的处理时间,如果有空队列,请跳过它并循环查找每个间隔中处理的物品数(每次超过一分钟)。我想知道是否有方法可以改善我编写的代码?非常感谢您的帮助。
public void incrementTime(double incLength) {
time += incLength; // increment time
double lambda = avgGrocPerMin * incLength;
Random rand = new Random();
// loop through each queue
for (int i = 0; i < lines.length; i++) {
if (lines[i].isEmpty()) { // skip empty queues
continue;
}
// loop to find number of groceries processed in interval
int k = 0;
double prob = rand.nextDouble();
while (true) {
// calc probability of exactly k groceries during interval (Poisson dist.)
double probK = Math.pow(lambda, k) * Math.exp(-lambda) / factorial(k);
if (probK <= prob) { // increment k until cumulative probability, prob, reached
k++;
prob -= probK;
} else {
break;
}
}
while (!lines[i].isEmpty() && k > 0) {
lines[i].decFront(); // decrement grocery count
k--;
}
}
this.display();
}