@blockchaincommons/rand - v1.0.0-beta.3
    Preparing search index...

    Interface RandomNumberGenerator

    A source of random 32-bit words, 64-bit words and bytes.

    nextU32 and nextU64 are two draws; which one a caller uses, and in what order, determines the bytes it produces. The samplers in /samplers draw nextU64() (masked to their width), as the reference implementation does for every width. The package's own generators return the low half of one 64-bit step from nextU32, as the reference's wrappers do; a third-party generator may relate the two draws however it likes.

    The contract is checked at every draw: nextU64() must return a bigint in [0, 2^64 - 1], nextU32() and nextU64Low32() an integer in [0, 2^32 - 1], and the member a helper or sampler calls must exist. A violation, or an undefined/null generator passed straight to a sampler, throws RandError InvalidGenerator naming the member; nothing is masked. A generator that meets the contract but never leaves Lemire's rejection zone (for example a constant draw of 0) makes a sampler loop forever, as it does in the reference.

    interface RandomNumberGenerator {
        nextU32(): number;
        nextU64(): bigint;
        fillBytes(dest: Uint8Array): void;
        nextU64Low32?(): number;
        fillBytesPacked?(dest: Uint8Array): void;
    }

    Implemented by

    Index
    • The next random 32-bit unsigned integer.

      Returns number

    • The next random 64-bit unsigned integer, a bigint in [0, 2^64 - 1].

      Returns bigint

    • Fill dest with random bytes.

      Parameters

      • dest: Uint8Array

      Returns void

    • Optional fast path for the samplers: the low 32 bits of the value nextU64() would have returned, advancing the generator exactly as nextU64() does, without allocating a bigint. Implement it only when that equality holds; the package's generators do.

      Returns number

    • Fills dest from the generator's packed byte stream — rand_core's fill_bytes layout (fill_bytes_via_next): eight little-endian bytes per 64-bit draw, a tail of five to seven bytes from one more 64-bit draw, a tail of one to four bytes from the next_u32 of the underlying rand_core generator. For SeededRng that generator is xoshiro256** behind the reference's wrapper, whose next_u32 is the high half of one more step — not nextU32(), which is the wrapper's low half. Only a generator whose fillBytes is a different stream needs to implement it (SeededRng does: its fillBytes is the reference's fill_random_data, one draw per byte); absent, the two streams are the same and callers use fillBytes.

      Parameters

      • dest: Uint8Array

      Returns void