XorShift.Javascript
#js
XorShiftはビットシフトを使った乱数生成アルゴリズム。シード付きの再現性がある乱数として利用できる。
つかいかた
code:usage.javascript
// このXorShiftの実装では、符号付きで10桁の値を返す
let random = new XorShift(100.); // てきとうなシード値を与える
let rndVal = random.next()/1000000000; // 以降、next()するたびに新しい値をとれる。1000000000で割って、おおよそ-2.2から2.2程度の範囲の値となる
code:XorShift.javascript
class XorShift {
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));
}
}