UNPKG

@augment-vir/common

Version:

A collection of augments, helpers types, functions, and classes for any JavaScript environment.

58 lines (57 loc) 2.28 kB
/** * A class that produces deterministic, pseudo random numbers based on a given seed. This uses the * Alea v0.9 algorithm from Johannes Baagøe. * * @category Random : Util * @category Package : @augment-vir/common * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common) */ export type SeededRandomState = [number, number, number, number]; /** * A class that produces deterministic, pseudo random numbers based on a given seed. This uses the * Alea v0.9 algorithm from Johannes Baagøe. * * @category Random * @category Package : @augment-vir/common * @example * * ```ts * import {SeededRandom} from '@augment-vir/common'; * * const random = SeededRandom.fromSeed('hello there'); * console.info(random.next()); // this value will always be the same * * const random2 = SeededRandom.fromState(random.exportState()); * console.info(random.next(), random2.next()); // both of these values will always be identical * * const random3 = random2.clone(); * console.info(random.next(), random2.next(), random3.next()); // all of these values will always be identical * ``` * * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common) */ export declare class SeededRandom { private readonly alea; /** Generate a new {@link SeededRandom} instance from a seed. */ static fromSeed(seed: string | number): SeededRandom; /** Generate a new {@link SeededRandom} instance from a specific state. */ static fromState(state: SeededRandomState): SeededRandom; /** * Exports the current state of the seeded random number generator so it can be cloned to * another instance. Use {@link SeededRandom.fromState} to consume this state. */ exportState(): SeededRandomState; /** * This constructor is private. Use {@link SeededRandom.fromSeed} or * {@link SeededRandom.fromState} instead. */ private constructor(); /** Generates the next deterministic pseudo random number. */ next(): number; /** * Create a {@link SeededRandom} copy with the same state as this one. This is the same as * calling {@link SeededRandom.exportState} and passing it directly into * {@link SeededRandom.fromState}. */ clone(): SeededRandom; }