-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathios-jp.html
More file actions
139 lines (115 loc) · 3.33 KB
/
ios-jp.html
File metadata and controls
139 lines (115 loc) · 3.33 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
127
128
129
130
131
132
133
134
135
136
137
138
139
<script>
const PROBABILITIES = {
0: 100,
1: 98,
2: 96,
3: 94,
4: 90,
5: 85,
6: 80,
7: 70,
8: 50,
9: 20,
10: 20,
11: 20,
12: 45,
13: 50,
14: 55,
15: 55,
16: 20,
17: 30,
18: 40,
19: 40,
}
let RUNS = 500000;
let values = [];
let frequency = {};
let total = 0;
let min = 0;
let max = 20;
class IOrun {
constructor(start = 0, end = 20) {
this.level = start;
this.end = end;
this.attempts = 0;
this.run();
}
run() {
do {
this.upgrade();
} while (this.level < this.end)
values.push(this.attempts);
total += this.attempts;
if (!frequency[this.attempts]) frequency[this.attempts] = 0;
frequency[this.attempts]++;
}
upgrade() {
let prob = Math.random() * 100;
if (this.level >= 3) this.attempts++;
if (prob <= PROBABILITIES[this.level]) {
// success
this.level++;
} else if (this.level >= 12) {
// failing above +12
this.level--;
}
}
}
// run attempts
function run() {
// reset
total = 0;
values = [];
frequency = {};
min = document.getElementById('min').value;
max = document.getElementById('max').value;
RUNS = document.getElementById('trials').value;
min = parseInt(min);
max = parseInt(max);
RUNS = parseInt(RUNS);
if (!max || min === NaN || max <= min || max > 20 || min < 0 || !RUNS || RUNS <= 0) return alert('Invalid parameters')
for (let i = 0; i < RUNS; i++) new IOrun(min, max);
const mean = total / RUNS;
console.log(values.length);
console.log(RUNS);
// calculate STD
let sum = 0;
for (const value of values) {
sum += Math.pow((value - mean), 2);
}
const std = Math.pow((sum * (1 / RUNS)), 0.5);
const mean_display = `${RUNS} simulations from +${min} to +${max} - IOs needed:<br />${mean} ± ${std}`;
// calculate percentile and median
values = [];
let keys = Object.keys(frequency).sort((a, b) => a - b); // make sure they're numbers and not strings
for (const key of keys) {
let occurences = frequency[key];
values.push(...new Array(occurences).fill().map(v => key));
}
let percentiles = [];
for (let i = 0; i < 10; i++) {
const index = Math.floor(0.1 * i * RUNS);
percentiles.push(`${i * 10}th percentile: ${values[index]}`);
}
percentiles.push(`Max: ${values[RUNS - 1]}`);
const percentile_display = percentiles.join('<br />');
document.getElementById('results').innerHTML = mean_display + '<br /><br /><br /><br />' + percentile_display
}
</script>
<body>
<h4>IO Probability calculator (Japanese version of TLoN)</h4>
<br />
<table style="width: 100%">
<tr>
<td style="border: 1px dashed black; border-radius: 10px; padding: 10px;">
Start: <input id="min" value="0"></input><br />
Finish: <input id="max" value="20"></input><br />
Trials: <input id="trials" value="500000"></input><br />
<button onclick="run()">Start!</button>
</td>
<td id="results" style="border: 1px dashed black; border-radius: 10px; padding: 10px;">
...
</td>
</tr>
</table>
</body>