Last active
July 3, 2019 07:54
-
-
Save nyawach/d2512c662bfdb7cdef73426f50e9fea9 to your computer and use it in GitHub Desktop.
seedで乱数固定可能な乱数生成クラス
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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