mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-29 03:44:40 +00:00
f6b739e263
strings with zod for safety
51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
export class PseudoRandom {
|
|
private m: number = 0x80000000; // 2**31
|
|
private a: number = 1103515245;
|
|
private c: number = 12345;
|
|
private state: number;
|
|
|
|
constructor(seed: number) {
|
|
this.state = seed % this.m;
|
|
}
|
|
|
|
/**
|
|
* Generates the next pseudorandom number.
|
|
* @returns A number between 0 (inclusive) and 1 (exclusive).
|
|
*/
|
|
next(): number {
|
|
this.state = (this.a * this.state + this.c) % this.m;
|
|
return this.state / this.m;
|
|
}
|
|
|
|
/**
|
|
* Generates a random integer between min (inclusive) and max (exclusive).
|
|
*/
|
|
nextInt(min: number, max: number): number {
|
|
return Math.floor(this.next() * (max - min) + min);
|
|
}
|
|
|
|
/**
|
|
* Generates a random float between min (inclusive) and max (exclusive).
|
|
*/
|
|
nextFloat(min: number, max: number): number {
|
|
return this.next() * (max - min) + min;
|
|
}
|
|
|
|
nextID(): string {
|
|
return this.nextInt(0, Math.pow(36, 8)) // 36^8 possibilities
|
|
.toString(36) // Convert to base36 (0-9 and a-z)
|
|
.padStart(8, '0'); // Ensure 8 chars by padding with zeros
|
|
}
|
|
|
|
randElement<T>(arr: T[]): T {
|
|
if (arr.length == 0) {
|
|
throw new Error('array must not be empty')
|
|
}
|
|
return arr[this.nextInt(0, arr.length)];
|
|
}
|
|
|
|
chance(odds: number): boolean {
|
|
return this.nextInt(0, odds) == 0
|
|
}
|
|
|
|
} |