-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
64 lines (64 loc) · 1.79 KB
/
index.ts
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
function strToHash(str: string) {
let [h1, h2, h3, h4] = [1779033703, 3144134277, 1013904242, 2773480762];
for (let i = 0, k; i < str.length; i++) {
k = str.charCodeAt(i);
[h1, h2, h3, h4] = [
h2 ^ Math.imul(h1 ^ k, 597399067),
h3 ^ Math.imul(h2 ^ k, 2869860233),
h4 ^ Math.imul(h3 ^ k, 951274213),
h1 ^ Math.imul(h4 ^ k, 2716044179),
];
}
[h1, h2, h3, h4] = [
Math.imul(h3 ^ (h1 >>> 18), 597399067),
Math.imul(h4 ^ (h2 >>> 22), 2869860233),
Math.imul(h1 ^ (h3 >>> 17), 951274213),
Math.imul(h2 ^ (h4 >>> 19), 2716044179),
];
(h1 ^= h2 ^ h3 ^ h4), (h2 ^= h1), (h3 ^= h1), (h4 ^= h1);
return [h1 >>> 0, h2 >>> 0, h3 >>> 0, h4 >>> 0];
}
export default class Random {
seed: unknown;
constructor(seed: unknown) {
this.seed = seed;
}
random = () => {
const str = String(this.seed);
let [a, b, c, d] = strToHash(str);
a |= 0;
b |= 0;
c |= 0;
d |= 0;
const t = (((a + b) | 0) + d) | 0;
d = (d + 1) | 0;
a = b ^ (b >>> 9);
b = (c + (c << 3)) | 0;
c = (c << 21) | (c >>> 11);
c = (c + t) | 0;
return (t >>> 0) / 4294967296;
};
shuffle = (arr: unknown[], reverse?: boolean) => {
const tempArr = [...arr];
if (!reverse) {
return new Array(arr.length)
.fill(undefined)
.map(
() =>
tempArr.splice(
Math.floor(Number(this.random()) * tempArr.length),
1
)[0]
);
} else {
const retArr = new Array(arr.length).fill(undefined);
const shuffled: number[] = this.shuffle(
new Array(arr.length).fill(undefined).map((val, index) => index)
);
if (shuffled) {
shuffled.map((val, index) => (retArr[val] = arr[index]));
return retArr;
} else return arr;
}
};
}