-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
plot.py
68 lines (54 loc) · 1.61 KB
/
plot.py
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
#! /usr/bin/env python3
import random
import matplotlib.pyplot as plt
import numpy as np
uList = [0] * 1000
for i in range(30):
random.seed(i)
for jobLength in range(1, 1001):
tickTotal = 200
runTotal = 2 * jobLength
jobs = 2
quantum = 1
jobList = [[0, jobLength, 100], [1, jobLength, 100]]
clock = 0
finishTime = []
# run jobs
for k in range(runTotal):
r = int(random.random() * 1000001)
winner = int(r % tickTotal)
current = 0
wjob = 0
wrun = 0
wtix = 0
for (job, runtime, tickets) in jobList:
current += tickets
if current > winner and runtime > 0:
(wjob, wrun, wtix) = (job, runtime, tickets)
break
# now do the accounting
if wrun >= quantum:
wrun -= quantum
else:
wrun = 0
jobList[wjob] = (wjob, wrun, wtix)
clock += quantum
# job completed!
if wrun == 0:
tickTotal -= wtix
jobs -= 1
finishTime.append(clock)
if jobs == 0:
break
u = round(finishTime[0] / finishTime[1], 2)
uList[jobLength - 1] += u
fig = plt.figure()
x = np.linspace(1, 1000, 1000)
plt.plot(x, [u / 30 for u in uList], color='orange')
plt.ylim(0, 1)
plt.margins(0)
plt.xlabel('Job Length')
plt.ylabel('Unfairness (Average)')
plt.title('Figure 9.2: Lottery Fairness Study')
plt.savefig('9.2.png', dpi=227)
plt.show()