Skip to content

Instantly share code, notes, and snippets.

@LarchLiu
Last active March 5, 2022 10:37
Show Gist options
  • Save LarchLiu/58f3f5697e007b57d508ee51e4f1fa4b to your computer and use it in GitHub Desktop.
Save LarchLiu/58f3f5697e007b57d508ee51e4f1fa4b to your computer and use it in GitHub Desktop.
XorShift by kotofurumiya
// 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));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment