-
Notifications
You must be signed in to change notification settings - Fork 0
/
sickboy.html
78 lines (53 loc) · 1.58 KB
/
sickboy.html
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
<body>
<div id="areaX">I am the ratmaster</div>
</body>
<script>
////////////////////////////////////////////////////////////
// pizzaface
//
////////////////////////////////////////////////////////////
//const trials = 100000;
const familySize = 5;
const chanceOfBoy = 1/2;
const chanceOfSickness = 1/4;
const startOnLoad = false;
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
const log = console.log;
const rand = (x=1) => [...crypto.getRandomValues(
new Uint32Array(1))
].map(v => v / (0xffffffff + 1))[0] * x;
let count = 0;
let matchFams = 0;
let looping = false;
function Kid (gen, sic) {
this.gender = gen;
this.sickness = sic;
}
function Family (size) {
this.kids = [...new Array(size)].map(() =>
new Kid(
rand() > chanceOfBoy ? 'boy' : 'girl',
rand() > chanceOfSickness ? 'healthy' : 'sick'
)
);
this.sickboys = this.kids.filter(k =>
k.gender === 'boy' && k.sickness === 'sick'
).length;
}
function animate () {
if (looping) {
areaX.innerHTML =
(matchFams += new Family(familySize).sickboys === 2 ? 1 : 0) +
' / '+ (++count) + ' → ' +
(100 * matchFams / count).toFixed(3) + '%';
looping = setTimeout(animate, 0);
}
}
window.addEventListener('keypress', e => {
if (e.code === 'Space') {
looping = looping? false : setTimeout(animate, 0);
}
});
!startOnLoad || (looping = setTimeout(animate, 0));
</script>