-
Notifications
You must be signed in to change notification settings - Fork 0
/
CashierDesk.java
74 lines (67 loc) · 1.78 KB
/
CashierDesk.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package week_7.wangwei;
import util.wangwei.MyQueue;
import java.util.Random;
/**
* 模拟收银台
* @author hoyoyes
*/
public class CashierDesk {
public static void main(String[] args) {
MyQueue<People> queue = new MyQueue<People>();
init(queue);
do{
double addNumber = poissonInteger(3);
int operaNumber = timesInteger(2);
for(int i=0; i<operaNumber&&queue.getSize()>0;i++){
queue.poll();
}
if(queue.getSize()<=0){break;}
for(int i=0; i<addNumber&&queue.getSize()<20; i++){
queue.offer(new People());
}
}while (!queue.isEmpty()&&queue.getSize()<20);
System.out.println(queue.getSize());
}
private static void init(MyQueue<People> queue){
double addNumber = poissonInteger(3);
for(int i=0; i<addNumber&&queue.getSize()<20; i++){
queue.offer(new People());
}
}
/**
* 利用泊松分布产生随机数
* @param lambda
* @return
*/
public static double poissonInteger(int lambda){
double x=0,b=1,c=Math.exp(-lambda),u;
do {
u=Math.random();
b *=u;
if(b>=c) {
x++;
}
}while(b>=c);
return Math.ceil(x);
}
/**
* 利用指数分布产生随机数
* @param lambda
* @return
*/
public static int timesInteger(int lambda){
Random random = new Random();
double u = random.nextDouble();
int x = 0;
double cdf = 0;
while (u >= cdf) {
x ++;
cdf = 1 - Math.exp(-1.0 * lambda * x);
}
return x;
}
}
class People{
String name;
String age;
}