Skip to content

Instantly share code, notes, and snippets.

@nyawach
Last active July 3, 2019 07:54
Show Gist options
  • Save nyawach/d2512c662bfdb7cdef73426f50e9fea9 to your computer and use it in GitHub Desktop.
Save nyawach/d2512c662bfdb7cdef73426f50e9fea9 to your computer and use it in GitHub Desktop.
seedで乱数固定可能な乱数生成クラス
// https://sbfl.net/blog/2017/06/01/javascript-reproducible-random/
class Random {
constructor(seed = 88675123) {
this.x = 123456789;
this.y = 362436069;
this.z = 521288629;
this.w = seed;
}
// XorShift
next() {
let t;
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));
}
// min以上max以下の乱数を生成する
nextInt(min = 0, max = 100000) {
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