createSeededRng function exported
Last updated: 2026-03-05T10:53:28.864Z
Location
Metrics
LOC: 48
Complexity: 3
Params: 1
Signature
createSeededRng(seed: string): : SeededRng
Summary
Create a seeded PRNG instance. Returns a SeededRng object with utility methods that draw from the same deterministic sequence. ts const rng = createSeededRng("demo-seed-42"); rng.next(); // 0 ≤ n < 1 rng.int(1, 100); // 1 ≤ n ≤ 100 rng.pick(["a","b"]) // "a" or "b" deterministically
Tags
#@example
Source Code
export function createSeededRng(seed: string): SeededRng {
let state = hashSeed(seed);
/** mulberry32 core — returns float [0, 1) */
function next(): number {
state |= 0;
state = (state + 0x6d2b79f5) | 0;
let t = Math.imul(state ^ (state >>> 15), 1 | state);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
}
return {
next,
int(min: number, max: number): number {
return Math.floor(next() * (max - min + 1)) + min;
},
pick<T>(array: readonly T[]): T {
return array[Math.floor(next() * array.length)];
},
shuffle<T>(array: readonly T[]): T[] {
const result = [...array];
for (let i = result.length - 1; i > 0; i--) {
const j = Math.floor(next() * (i + 1));
[result[i], result[j]] = [result[j], result[i]];
}
return result;
},
char(charset: string): string {
return charset[Math.floor(next() * charset.length)];
},
string(
length: number,
charset = "abcdefghijklmnopqrstuvwxyz0123456789",
): string {
let result = "";
for (let i = 0; i < length; i++) {
result += charset[Math.floor(next() * charset.length)];
}
return result;
},
};
}
Members
| Name | Kind | Visibility | Status | Signature |
|---|---|---|---|---|
| next | function | - | next(): : number |
Dependencies (Outgoing)
| Target | Type |
|---|---|
| hashSeed | calls |
Impact (Incoming)
| Source | Type |
|---|---|
| OrchestratorCallbacks | uses |
| start | calls |