/
EventSimulator.java
51 lines (45 loc) · 1.51 KB
/
EventSimulator.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
import java.util.Scanner;
public class EventSimulator {
public static void main(String[] args) {
final int ARRIVAL_MEAN=5;
int simulationLength=0, minute=0, nextArrivalTime, callCount=0;
Scanner input = new Scanner(System.in);
while (simulationLength<=0) {
System.out.print("How many minutes long is the simulation? ");
while (!input.hasNextInt()) {
input.next();
System.out.print("Please enter an integer: ");
}
simulationLength = input.nextInt();
}
nextArrivalTime = minute + getRandInt('E', ARRIVAL_MEAN, 0);
while(minute<=simulationLength) {
while ((minute == nextArrivalTime) && (minute<=simulationLength)) {
callCount++;
System.out.println("Minute:"+minute+" Event#"+callCount);
nextArrivalTime=minute+getRandInt('E', ARRIVAL_MEAN, 0);
}
minute++;
}
System.out.println("Number of events = " + callCount);
}
public static int getRandInt(char type, int x, int y) {
int num;
switch (type) {
case 'U': case 'u': // Uniform Distribution
num = (int)(x + (Math.random()*(y+1-x)));
break;
case 'E': case 'e': // Exponential Distribution
num = (int)(-1*x*Math.log(Math.random()));
break;
case 'N': case 'n': // Normal Distribution
num = (int)( x +
(y * Math.cos(2 * Math.PI * Math.random()) *
Math.sqrt(-2 * Math.log(Math.random()))));
break;
default: // Uniform Distribution
num = (int)(x + (Math.random()*(y+1-x)));
}
return num;
}
}