This commit is contained in:
Schmelczer András 2020-01-06 21:40:25 +01:00
parent f054546aa6
commit 48a55a4a97
51 changed files with 604 additions and 577 deletions

View file

@ -2,16 +2,17 @@ export class Random {
public constructor(private seed: number) {}
public get next(): number {
// result is in [0, 1)
return (
((2 ** 31 - 1) & (this.seed = Math.imul(48271, this.seed))) / 2 ** 31
);
}
public choose<T>(list: Array<T>): T {
return list[this.randomInInterval(0, list.length)];
return list[Math.floor(this.randomInInterval(0, list.length))];
}
public randomInInterval(aClosed: number, bOpen: number): number {
return Math.floor((bOpen - aClosed) * this.next) + aClosed;
return (bOpen - aClosed) * this.next + aClosed;
}
}