-
Notifications
You must be signed in to change notification settings - Fork 0
/
2017-04-14.jl
61 lines (57 loc) · 1.38 KB
/
2017-04-14.jl
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
function vacate!(yr::Float64, time::Float64, bench::Array{Array{Float64, 1}, 1})
for pair in bench
if pair[1] == 1.0
if yr < pair[2] <= yr + time
pair[1] = 0.0
end
end
end
end
function replace!(yr::Float64, bench::Array{Array{Float64, 1}, 1}, reps::Array{Int64})
vac_time = 0.0
for pair in bench
if pair[1] == 0.0
d_time = pair[2]
if yr - d_time >= 0.0
vac_time += yr - d_time
end
pair[1] = 1.0
pair[2] = 40 * rand() + yr
reps[1] = reps[1] + 1
while pair[2] <= yr + 2.0
pair[2] = 40 * rand() + yr
reps[1] = reps[1] + 1
end
end
end
return vac_time
end
function fouryr!(yr::Float64, bench::Array{Array{Float64, 1}, 1}, reps::Array{Int64})
p, s = rand([true, false], 2)
vacate!(yr, 2.0, bench)
vacant_time = 0.0
if (p && s) || (!p && !s)
vacant_time += replace!(yr, bench, reps)
end
yr += 2.0
s = rand([true, false])
vacate!(yr, 2.0, bench)
if (p && s) || (!p && !s)
vacant_time += replace!(yr, bench, reps)
end
return vacant_time
end
function simulation(admins::Int64)
reps = [0]
year = 0.0
bench = [[1.0, (rand() * 40)] for i in 1:9]
total_vacancy = 0.0
for i in 1:admins
total_vacancy += fouryr!(year, bench, reps)
year += 4.0
end
display(bench)
display(year)
display(total_vacancy / reps[1])
return total_vacancy / (admins * 4.0)
end