UNPKG

@spissvinkel/alea

Version:
104 lines 4.06 kB
/** * An Alea PRNG */ export interface AleaPRNG { /** * @returns a pseudo-random floating-point number in the interval `[0, 1)` (like `Math.random()`) */ random: () => number; /** * @returns a pseudo-random unsigned 32-bit integer in the interval `[0, 2^32)` */ uint32: () => number; /** * @returns a pseudo-random full 53-bit fraction in the interval `[0, 1)`. (Slower than [[`AleaPRNG.random`]] but higher precision) */ fract53: () => number; /** * Creates utility functions based on this PRNG, e.g.: * ``` * const alea: AleaPRNG = mkAlea(); * const nextBool: () => boolean = alea.nextT(n => n < 0.5); * // calling `nextBool()` now returns `true` or `false` at random * ``` * @returns a function that, when invoked, transforms the next pseudo-random number `n` to a `T` using the provided function `f` */ nextT: <T>(f: (n: number) => T) => () => T; /** * @returns a copy of the current state of this PRNG, which can be saved and then later passed to the [[`restoreAlea`]] function */ getState: () => AleaState; } /** * The internal state of an Alea PRNG */ export interface AleaState { s0: number; s1: number; s2: number; c: number; } /** * Initialize a new PRNG * * @param seed an optional seed value * @returns an initialized PRNG */ export declare const mkAlea: (seed?: string) => AleaPRNG; /** * Initialize a new PRNG from a previously saved state, effectively allowing the previous PRNG to be resumed * * @param state a state object, probably retrieved from an existing PRNG with [[`AleaPRNG.getState`]] * @returns an initialized PRNG */ export declare const restoreAlea: (state: AleaState) => AleaPRNG; /** * @param state a state object, probably created with [[`mkState`]]. Will be updated as a side effect * @returns a pseudo-random floating-point number in the interval `[0, 1)` (like `Math.random()`) */ export declare const random: (state: AleaState) => number; /** * @param state a state object, probably generated by [[`mkState`]]. Will be updated as a side effect * @returns a pseudo-random unsigned 32-bit integer in the interval `[0, 2^32)` */ export declare const uint32: (state: AleaState) => number; /** * @param state a state object, probably generated with [[`mkState`]]. Will be updated as a side effect * @returns a pseudo-random full 53-bit fraction in the interval `[0, 1)`. (Slower than [[`random`]] but higher precision) */ export declare const fract53: (state: AleaState) => number; /** * Creates utility functions based on the [[`random`]] function, e.g.: * ``` * const state: AleaState = mkState('123'); * const nextBool: (state: AleaState) => boolean = nextT(n => n < 0.5); * // calling `nextBool(state)` now returns `true` or `false` at random * ``` * @param f a function to transform a number in the interval `[0, 1)` to a value of type `T` * @param state a state object, probably generated with [[`mkState`]]. Will be updated as a side effect * @returns a function that, when invoked with a state, transforms the next pseudo-random number `n` to a `T` using the provided function `f` */ export declare const nextT: <T>(f: (n: number) => T) => (state: AleaState) => T; /** * Initializes a new PRNG state object using the provided seed value * * @param seed a seed value * @returns a new state object based on the provided `seed` */ export declare const mkState: (seed: string) => AleaState; /** * Initializes a recycled PRNG state object using the provided seed value * * @param seed a seed value * @param state a state object to initialize * @returns the provided `state` object initialized using the provided `seed` */ export declare const initState: (seed: string, state: AleaState) => AleaState; /** * Creates an uninitialized PRNG state object. * Must be initialized with [[`initState`]] before use * * @returns a new, uninitialized state object */ export declare const emptyState: () => AleaState; //# sourceMappingURL=index.d.ts.map