-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulation.java
More file actions
127 lines (59 loc) · 2.27 KB
/
Simulation.java
File metadata and controls
127 lines (59 loc) · 2.27 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//The program answers the question:
//If there are two buses, one that comes every 12 minutes and one that comes every 8, when the earliest bus is expected to come?
//The simulation returns around 3.11 minutes
//This is true as it can be proved mathimatically:
//f(t) = [(8-t) + (12-t)]/8*12
//f(t) = (20-2t)/96
//E(T)=\int_0^{8} t*f(t) dt
//E(T)=\int_0^{8} t*((20-2t)/96) dt
//E(T)=\int_0^{8} t*((20t-2t^2)/96) dt
//E(T)=[(10t^2-(2t^3)/3)/96]_0^{8}
//E(T)=[(10*64-(2*512)/3)/96]-0
//E(T) = 3.11111111111 which agrees with our simulation as well
/**
* @author Andreas Loizou
*
*/
public class BusSimulation {
final static Random random = new Random();
/**
* @param UPPER_RANGE The upper bound of the number.
* @return A number between 0 and UPPER_RANGE included
*/
public static double getRandomValue(final int UPPER_BOUND) {
return random.nextDouble() * UPPER_BOUND;
}
/**
* @param cycles How many times the simulation of the two buses will run
* @return The average time the first bus needs
*/
public static double simulation(final int cycles) {
//cycles cannot be negative
if (cycles < 0) {
throw new IllegalArgumentException(
"cycles input variable is negative");
}
//Prevent division by zero
if (cycles == 0) {
return 0;
}
//Start the calculation. Get the minimum time between the two buses
double sum = 0;
for (int i = 0; i < cycles; i++) {
sum += Math.min(get_random_value(12), get_random_value(8));
}
//Return the average time
return sum / cycles;
}
/**
* @param args Not used
*/
public static void main(String[] args) {
//Run several cycles of simulations
final int MAX_NUMBER_OF_SIMULATIONS = 1000000;
final int CYCLE_STEP = 10000;
for (int i = 0; i < MAX_NUMBER_OF_SIMULATIONS; i += CYCLE_STEP) {
System.out.printf(i + "\t: " + "%.2f" + "\n", simulation(i));
}
}
}