-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdemo.html
59 lines (51 loc) · 1.41 KB
/
demo.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
<!DOCTYPE html>
<pre id=log></pre>
<script>
function log(message) {document.querySelector("#log").textContent += message + "\n";};
Math.seededPRNG = class seededPRNG {
#state;
constructor(init) {
this.#state = this.constructor.#stateFromInit(init);
}
static #stateFromInit(init) {
if(Number.isFinite(init)) {
return Math.floor(init);
}
if((init instanceof Uint8Array || init instanceof Int8Array || init instanceof Uint8ClampedArray) && init.byteLength == 1) {
console.log(init)
return init[0];
}
console.log(init)
throw new DOMException("Not a valid prng init value");
}
state() {
return this.#state;
}
setState(init) {
this.#state = this.constructor.stateFromInit(init);
}
random() {
// Really dumb LCG, using the glibc constants
const a = 1103515245;
const c = 12345;
const mod = Math.pow(2, 31) - 1;
let seed = (this.#state) % mod;
seed = (seed * a + c) % mod;
this.#state = seed;
return (seed & 0x3fffffff)/0x3fffffff;
}
randomSeed() {
return Math.floor(this.random() * 2**32);
}
}
var counts = [0,0,0,0,0,0,0,0,0,0];
var limit = 1e5;
var prng = new Math.seededPRNG(new Uint8Array([1]));
for(var i = 0; i < limit; i++) {
counts[Math.floor(prng.random()*10)]++;
}
for(let i = 0; i < counts.length; i++) {
c = counts[i];
log(`${i}: ${String(c).padStart(5)} ${(c/limit*100).toFixed(1).padStart(5)}%`);
}
</script>