-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathutil.ts
37 lines (33 loc) · 785 Bytes
/
util.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
// XorShift by kotofurumiya
// https://sbfl.net/blog/2017/06/01/javascript-reproducible-random/
export class Random {
x: number;
y: number;
z: number;
w: number;
constructor(seed = 88675123) {
this.x = 123456789;
this.y = 362436069;
this.z = 521288629;
this.w = seed;
}
// XorShift
next() {
const t = this.x ^ (this.x << 11);
this.x = this.y;
this.y = this.z;
this.z = this.w;
return this.w = (this.w ^ (this.w >>> 19)) ^ (t ^ (t >>> 8));
}
nextInt(min: number, max: number) {
const r = Math.abs(this.next());
return min + (r % (max + 1 - min));
}
}
export function calcChecksum(str: string) {
let checksum = 0;
for (let i = 0; i < str.length; i++) {
checksum += str.charCodeAt(i);
}
return checksum;
}