@haelp/teto
Version:
A typescript-based controllable TETR.IO client.
57 lines (56 loc) • 1.49 kB
JavaScript
export class RNG {
static MODULUS = 2147483647;
static MULTIPLIER = 16807;
static MAX_FLOAT = 2147483646;
value;
index = 0;
constructor(seed){
this.value = seed % RNG.MODULUS;
if (this.value <= 0) {
this.value += RNG.MAX_FLOAT;
}
this.next = this.next.bind(this);
this.nextFloat = this.nextFloat.bind(this);
this.shuffleArray = this.shuffleArray.bind(this);
this.updateFromIndex = this.updateFromIndex.bind(this);
this.clone = this.clone.bind(this);
}
next() {
this.index++;
return this.value = RNG.MULTIPLIER * this.value % RNG.MODULUS;
}
nextFloat() {
return (this.next() - 1) / RNG.MAX_FLOAT;
}
shuffleArray(array) {
if (array.length === 0) {
return array;
}
for(let i = array.length - 1; i !== 0; i--){
const r = Math.floor(this.nextFloat() * (i + 1));
[array[i], array[r]] = [
array[r],
array[i]
];
}
return array;
}
get seed() {
return this.value;
}
set seed(value) {
this.value = value % RNG.MODULUS;
if (this.value <= 0) {
this.value += RNG.MAX_FLOAT;
}
}
updateFromIndex(index) {
while(this.index < index){
this.next();
}
}
clone() {
return new RNG(this.value);
}
}
//# sourceMappingURL=index.js.map