Skip to content

Shuffle reproducibly from a seed

Pass a seeded random source to shuffled, and the same seed gives the same order on every machine.

import {
function splitmix32(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

https://github.com/skeeto/hash-prospector hash-prospector

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

https://dl.acm.org/doi/10.1145/2660193.2660195 Fast Splittable Pseudorandom Number Generators

.

This generator is not cryptographically secure. Use crypto.getRandomValues() when unpredictability matters.

@param ― seed - Initial seed, coerced to a signed 32-bit integer.

@returns ― A stateful function that returns the next value in the range [0, 1).

splitmix32
} from 'knotkit/prng'
import {
function shuffled<T>(random: RandomSource, values: readonly T[]): readonly T[]

Return a uniformly shuffled copy of values using Fisher-Yates.

Runs in O(values.length) time and uses O(values.length) space for the copy. The input is never mutated.

@throws ― {PrngInvalidInputError} With code INVALID_SOURCE_VALUE when the random source returns a value outside [0, 1).

shuffled
} from 'knotkit/sampling'
const
const players: string[]
players
= ['ada', 'grace', 'alan', 'edsger', 'barbara']
const
const first: readonly string[]
first
=
shuffled<string>(random: RandomSource, values: readonly string[]): readonly string[]

Return a uniformly shuffled copy of values using Fisher-Yates.

Runs in O(values.length) time and uses O(values.length) space for the copy. The input is never mutated.

@throws ― {PrngInvalidInputError} With code INVALID_SOURCE_VALUE when the random source returns a value outside [0, 1).

shuffled
(
function splitmix32(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

https://github.com/skeeto/hash-prospector hash-prospector

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

https://dl.acm.org/doi/10.1145/2660193.2660195 Fast Splittable Pseudorandom Number Generators

.

This generator is not cryptographically secure. Use crypto.getRandomValues() when unpredictability matters.

@param ― seed - Initial seed, coerced to a signed 32-bit integer.

@returns ― A stateful function that returns the next value in the range [0, 1).

splitmix32
(2026),
const players: string[]
players
)
const
const again: readonly string[]
again
=
shuffled<string>(random: RandomSource, values: readonly string[]): readonly string[]

Return a uniformly shuffled copy of values using Fisher-Yates.

Runs in O(values.length) time and uses O(values.length) space for the copy. The input is never mutated.

@throws ― {PrngInvalidInputError} With code INVALID_SOURCE_VALUE when the random source returns a value outside [0, 1).

shuffled
(
function splitmix32(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

https://github.com/skeeto/hash-prospector hash-prospector

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

https://dl.acm.org/doi/10.1145/2660193.2660195 Fast Splittable Pseudorandom Number Generators

.

This generator is not cryptographically secure. Use crypto.getRandomValues() when unpredictability matters.

@param ― seed - Initial seed, coerced to a signed 32-bit integer.

@returns ― A stateful function that returns the next value in the range [0, 1).

splitmix32
(2026),
const players: string[]
players
)
const first: readonly string[]
first
// ['ada', 'grace', 'edsger', 'barbara', 'alan']
const first: readonly string[]
first
.
ReadonlyArray<string>.join(separator?: string): string

Adds all the elements of an array separated by the specified separator string.

@param ― separator A string used to separate one element of an array from the next in the resulting String. If omitted, the array elements are separated with a comma.

join
() ===
const again: readonly string[]
again
.
ReadonlyArray<string>.join(separator?: string): string

Adds all the elements of an array separated by the specified separator string.

@param ― separator A string used to separate one element of an array from the next in the resulting String. If omitted, the array elements are separated with a comma.

join
() // true
const players: string[]
players
// ['ada', 'grace', 'alan', 'edsger', 'barbara'], the input is untouched

Nothing in Knotkit seeds itself. Record the seed with the result, and anyone can replay it.

Modules: knotkit/sampling · knotkit/prng · API reference