Cin-browser interpreter · runs locally
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
#include <stdio.h> #include <math.h> long state = 11; long lcg() { state = (1103515245L * state + 12345L) % 2147483648L; return state; } double uniform() { return (double)lcg() / 2147483648.0; } double exponential(double rate) { double u = uniform(); if (u < 1e-12) u = 1e-12; return -log(u) / rate; } int main() { double lambda = 4.0, mu = 5.0; double t = 0.0; double nextArrival = exponential(lambda); double nextDeparture = -1.0; int queue = 0, served = 0; double totalWait = 0, busyTime = 0; while (served < 1000) { if (nextArrival < nextDeparture || nextDeparture < 0) { t = nextArrival; if (queue == 0) nextDeparture = t + exponential(mu); queue++; nextArrival = t + exponential(lambda); } else { t = nextDeparture; queue--; served++; totalWait += t; if (queue > 0) { nextDeparture = t + exponential(mu); } else { nextDeparture = -1; } } } busyTime = t; printf("served = %d\n", served); printf("utilization = %.3f (rho = 0.8)\n", busyTime > 0 ? 1.0 : 0.0); printf("avg wait = %.3f\n", totalWait / served); return 0; }
47 lines
Output
Write some code, then press Run. Stdout and errors appear here.