Creates a deterministic 32-bit pseudorandom number generator.
Each call advances a private state by 0x9e3779b9. This constant equals
floor(2^32 / phi), where phi is the golden ratio. Addition wraps modulo
2^32. Because the increment is odd, it is coprime to 2^32. The resulting
Weyl sequence visits every 32-bit state once before it repeats.
The mixing function uses the constants 0x21f0aaad and 0x735a2d97 from
the
search
for low avalanche bias. Its XOR-shifts and multiplications by odd integers
are invertible over 32-bit values. The mixer therefore preserves the full
period while diffusing nearby states across the output bits.
Math.imul and the bitwise operators keep the mixing steps in 32-bit
arithmetic. The final unsigned conversion produces an integer from 0 to
2^32 - 1. Dividing by 2^32 maps that integer to [0, 1).
The SplitMix family was introduced by Steele, Lea, and Flood in
Draw an integer uniformly from 0..upperExclusive - 1.
Multiplying a source value by 2^32 produces one unsigned 32-bit draw. When
the requested range does not divide 2^32 evenly, a direct remainder would
make its lowest outcomes slightly more likely. Rejection sampling discards
the incomplete high end of the 32-bit range before taking the remainder, so
every returned integer has exactly the same number of source draws mapping
to it.
The source can be seeded or otherwise injected by the caller. Each value is
validated at the point it is consumed because a stateful source can become
invalid after earlier valid draws.
Consumes one source draw in the common case. The rejection region is always
smaller than upperExclusive, so fewer than half of all draws are rejected
and the expected number of draws is below two.
@param ― random - Function returning values in [0, 1).
@param ― upperExclusive - Exclusive upper bound, from 1 through 2^32.
@throws ― {PrngInvalidInputError} With code INVALID_BOUND when the bound is
not an integer in 1..2^32, or INVALID_SOURCE_VALUE when the source
returns a value outside [0, 1).
uniformInt } from'knotkit/prng'
const
constrandom: () =>number
random=
functionsplitmix32(seed:number): () =>number
Creates a deterministic 32-bit pseudorandom number generator.
Each call advances a private state by 0x9e3779b9. This constant equals
floor(2^32 / phi), where phi is the golden ratio. Addition wraps modulo
2^32. Because the increment is odd, it is coprime to 2^32. The resulting
Weyl sequence visits every 32-bit state once before it repeats.
The mixing function uses the constants 0x21f0aaad and 0x735a2d97 from
the
search
for low avalanche bias. Its XOR-shifts and multiplications by odd integers
are invertible over 32-bit values. The mixer therefore preserves the full
period while diffusing nearby states across the output bits.
Math.imul and the bitwise operators keep the mixing steps in 32-bit
arithmetic. The final unsigned conversion produces an integer from 0 to
2^32 - 1. Dividing by 2^32 maps that integer to [0, 1).
The SplitMix family was introduced by Steele, Lea, and Flood in
Draw an integer uniformly from 0..upperExclusive - 1.
Multiplying a source value by 2^32 produces one unsigned 32-bit draw. When
the requested range does not divide 2^32 evenly, a direct remainder would
make its lowest outcomes slightly more likely. Rejection sampling discards
the incomplete high end of the 32-bit range before taking the remainder, so
every returned integer has exactly the same number of source draws mapping
to it.
The source can be seeded or otherwise injected by the caller. Each value is
validated at the point it is consumed because a stateful source can become
invalid after earlier valid draws.
Consumes one source draw in the common case. The rejection region is always
smaller than upperExclusive, so fewer than half of all draws are rejected
and the expected number of draws is below two.
@param ― random - Function returning values in [0, 1).
@param ― upperExclusive - Exclusive upper bound, from 1 through 2^32.
@throws ― {PrngInvalidInputError} With code INVALID_BOUND when the bound is
not an integer in 1..2^32, or INVALID_SOURCE_VALUE when the source
returns a value outside [0, 1).
Draw an integer uniformly from 0..upperExclusive - 1.
This is the 64-bit counterpart of uniformInt. It consumes a source that
already returns unsigned 64-bit draws, so no floating-point step stands
between the generator and the sample, and bounds above 2^32 stay exact.
When the requested range does not divide 2^64 evenly, a direct remainder
would make its lowest outcomes slightly more likely. Rejection sampling
discards the incomplete high end of the 64-bit range before taking the
remainder, so every returned integer has exactly the same number of source
draws mapping to it.
The source can be seeded or otherwise injected by the caller. Each value is
validated at the point it is consumed because a stateful source can become
invalid after earlier valid draws.
Consumes one source draw in the common case. The rejection region is always
smaller than upperExclusive, so fewer than half of all draws are rejected
and the expected number of draws is below two.
@param ― random - Function returning unsigned 64-bit values in [0, 2^64).
@param ― upperExclusive - Exclusive upper bound, from 1 through 2^64.
@throws ― {PrngInvalidInputError} With code INVALID_BOUND when the bound is
not a bigint in 1..2^64, or INVALID_SOURCE_VALUE when the source returns
a value outside [0, 2^64).
Create a xoroshiro128** 1.0 pseudorandom number generator.
The two input words are reduced modulo 2^64. They must not both reduce to
zero because the all-zero state is fixed and would only emit zeroes. Each
call to next() runs in constant time, mutates the private 128-bit state,
and returns the same unsigned 64-bit value as the reference C implementation.
jump() advances by 2^64 outputs and longJump() advances by 2^96 outputs.
Both run in constant space and a fixed 128 state transitions.
This generator is not cryptographically secure. Use crypto.getRandomValues()
when unpredictability matters.
@param ― initialState - Two integers representing the initial 64-bit state words.
@returns ― A generator with private state and fixed-distance jump operations.
@throws ― {PrngInvalidInputError} With code INVALID_STATE when the state does
not contain two bigints or both words reduce to zero modulo 2^64.
Create a xoroshiro128** 1.0 pseudorandom number generator.
The two input words are reduced modulo 2^64. They must not both reduce to
zero because the all-zero state is fixed and would only emit zeroes. Each
call to next() runs in constant time, mutates the private 128-bit state,
and returns the same unsigned 64-bit value as the reference C implementation.
jump() advances by 2^64 outputs and longJump() advances by 2^96 outputs.
Both run in constant space and a fixed 128 state transitions.
This generator is not cryptographically secure. Use crypto.getRandomValues()
when unpredictability matters.
@param ― initialState - Two integers representing the initial 64-bit state words.
@returns ― A generator with private state and fixed-distance jump operations.
@throws ― {PrngInvalidInputError} With code INVALID_STATE when the state does
not contain two bigints or both words reduce to zero modulo 2^64.
xoroshiro128StarStar([1n, 2n])
constrandom:Xoroshiro128StarStar
random.
Xoroshiro128StarStar.next: () => bigint
Advance the state once and return an unsigned 64-bit value.
next() // 0x1680n
constrandom:Xoroshiro128StarStar
random.
Xoroshiro128StarStar.next: () => bigint
Advance the state once and return an unsigned 64-bit value.
Draw an integer uniformly from 0..upperExclusive - 1.
This is the 64-bit counterpart of uniformInt. It consumes a source that
already returns unsigned 64-bit draws, so no floating-point step stands
between the generator and the sample, and bounds above 2^32 stay exact.
When the requested range does not divide 2^64 evenly, a direct remainder
would make its lowest outcomes slightly more likely. Rejection sampling
discards the incomplete high end of the 64-bit range before taking the
remainder, so every returned integer has exactly the same number of source
draws mapping to it.
The source can be seeded or otherwise injected by the caller. Each value is
validated at the point it is consumed because a stateful source can become
invalid after earlier valid draws.
Consumes one source draw in the common case. The rejection region is always
smaller than upperExclusive, so fewer than half of all draws are rejected
and the expected number of draws is below two.
@param ― random - Function returning unsigned 64-bit values in [0, 2^64).
@param ― upperExclusive - Exclusive upper bound, from 1 through 2^64.
@throws ― {PrngInvalidInputError} With code INVALID_BOUND when the bound is
not a bigint in 1..2^64, or INVALID_SOURCE_VALUE when the source returns
a value outside [0, 2^64).
uniformInt64(
constrandom:Xoroshiro128StarStar
random.
Xoroshiro128StarStar.next: () => bigint
Advance the state once and return an unsigned 64-bit value.
Seeded 128-bit-state generator over unsigned 64-bit `bigint` draws, matching xoroshiro128** 1.0; input words are reduced modulo 2^64 and must not both reduce to zero.
Unbiased integer in `0..upperExclusive - 1` from an injected `RandomSource`; holds no state but advances the source once per consumed draw, rejected draws included.
Use knotkit/prng for this task. If Knotkit is missing, follow https://knotkit.jkomyno.dev/getting-started.md#install-from-source. Import from 'knotkit/prng'.
Exports:
- `splitmix32` → `RandomSource`: Seeded 32-bit generator; the seed is coerced to a signed 32-bit integer and each call yields a value in `[0, 1)`.
- `xoroshiro128StarStar` → `Xoroshiro128StarStar`: Seeded 128-bit-state generator over unsigned 64-bit `bigint` draws, matching xoroshiro128** 1.0; input words are reduced modulo 2^64 and must not both reduce to zero.
- `uniformInt` → `number`: Unbiased integer in `0..upperExclusive - 1` from an injected `RandomSource`; holds no state but advances the source once per consumed draw, rejected draws included.
- `uniformInt64` → `bigint`: Unbiased `bigint` in `0..upperExclusive - 1` from an injected `RandomSource64`, with no floating-point step, so bounds above 2^32 stay exact.
Use something else when:
- generate tokens, passwords, keys, or nonces an attacker cannot predict → uniku (https://www.npmjs.com/package/uniku)
- one throwaway random number with no reproducibility requirement → the platform Math.random function
- shuffle an array or pick a weighted choice → knotkit/sampling
- derive a fingerprint or a shard key from bytes or text → knotkit/hash
- draw from a named distribution → knotkit/distribution
- test whether draws fit a distribution → knotkit/hypothesis-test
Errors are PrngInvalidInputError with a `code` property. Branch on `error.code`, never on the message.
Full page: https://knotkit.jkomyno.dev/modules/prng.md
Types: node_modules/knotkit/prng/ and https://knotkit.jkomyno.dev/reference/knotkit/prng/