32 lines
829 B
JavaScript
32 lines
829 B
JavaScript
export default class Random {
|
|
|
|
static instances = 0;
|
|
|
|
constructor(seed = Date.now() % 1000000000 ^ Random.instances++ * 1000) {
|
|
this.mask = 0xffffffff;
|
|
this.setSeed(seed);
|
|
}
|
|
|
|
nextBoolean() {
|
|
return this.nextFloat() > 0.5;
|
|
}
|
|
|
|
nextInt(max = 0x7fffffff) {
|
|
return Math.floor(this.nextFloat() * (max + 1));
|
|
}
|
|
|
|
nextFloat() {
|
|
this.m_z = (36969 * (this.m_z & 65535) + (this.m_z >>> 16)) & this.mask;
|
|
this.m_w = (18000 * (this.m_w & 65535) + (this.m_w >>> 16)) & this.mask;
|
|
|
|
let result = ((this.m_z << 16) + (this.m_w & 65535)) >>> 0;
|
|
result /= 4294967296;
|
|
return result;
|
|
}
|
|
|
|
setSeed(seed) {
|
|
this.seed = seed;
|
|
this.m_w = (123456789 + seed) & this.mask;
|
|
this.m_z = (987654321 - seed) & this.mask;
|
|
}
|
|
} |