You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Deterministic, reproducible random number generation using a seed. Generate numbers, booleans, dates, strings, UUIDs, colors, coordinates, and more — all reproducible from a seed value.
Installation
npm install @chaisser/random-seed
# or
yarn add @chaisser/random-seed
# or
pnpm add @chaisser/random-seed
Quick Start
import{createRNG}from'@chaisser/random-seed';constrng=createRNG(42);rng.nextInt(1,100);// deterministic integerrng.nextFloat(0,10);// deterministic floatrng.nextBool(0.3);// deterministic booleanrng.uuid();// deterministic UUIDrng.shuffle([1,2,3,4]);// deterministic shufflerng.pick(['a','b','c']);// deterministic pick// Same seed always produces same sequenceconsta=createRNG(42);constb=createRNG(42);a.nextInt(1,100)===b.nextInt(1,100);// true
API Reference
createRNG(seed?)
Creates a seeded random number generator. Accepts number or string seeds. Returns a SeededRNG object.
RNG Methods
Method
Description
next()
Float in [0, 1)
nextInt(min, max)
Integer in [min, max]
nextFloat(min?, max?)
Float in [min, max] (default 0, 1)
nextBool(probability?)
Boolean (default p=0.5)
nextDate(start, end)
Random Date in range
pick(array)
Random element
pickMultiple(array, count)
Multiple random elements (with replacement)
shuffle(array)
Shuffled copy (Fisher-Yates)
weighted(items)
Weighted random pick
uuid()
UUID v4 string
hex(bytes?)
Hex string (default 8 bytes = 16 chars)
alpha(length)
Alphabetic string
alphanumeric(length)
Alphanumeric string
reset()
Reset to initial seed state
getSeed()
Return the seed value
One-shot Helpers
Each method has a seeded* helper that creates a temporary RNG, calls once, and returns:
Function
Description
seededRandom(seed?)
Single float [0, 1)
seededInt(min, max, seed?)
Single int
seededFloat(min, max, seed?)
Single float
seededBool(probability, seed?)
Single bool
seededPick(array, seed?)
Single pick
seededPickMultiple(array, count, seed?)
Multiple picks
seededShuffle(array, seed?)
Shuffled copy
seededWeighted(items, seed?)
Weighted pick
seededUuid(seed?)
UUID
seededHex(bytes, seed?)
Hex string
seededAlpha(length, seed?)
Alpha string
seededAlphanumeric(length, seed?)
Alphanumeric string
seededDate(start, end, seed?)
Random date
Distributions
Function
Description
gaussian(mean?, stdDev?, rng?)
Box-Muller normal distribution
gaussianArray(count, mean?, stdDev?, rng?)
Array of gaussian values
exponential(lambda?, rng?)
Exponential distribution
Array Generators
Function
Description
intArray(min, max, count, rng?)
Array of random ints
floatArray(min, max, count, rng?)
Array of random floats
boolArray(count, probability?, rng?)
Array of random bools
uniqueInts(min, max, count, rng?)
Array of unique random ints
Color
Function
Description
randomColor(rng?)
Hex color string (#rrggbb)
randomRgb(rng?)
{ r, g, b } (0-255)
randomHsl(rng?)
{ h, s, l } (h: 0-360, s/l: 0-100)
Coordinates & Network
Function
Description
randomCoordinate(latRange, lngRange, rng?)
{ lat, lng }
randomIP(rng?)
IPv4 string
Dice
Function
Description
randomDice(sides?, rng?)
Roll one die (default 6 sides)
randomCoin(rng?)
'heads' or 'tails'
rollDice(count, sides, rng?)
Roll N dice
rollDiceSum(count, sides, rng?)
Sum of N dice
Examples
Reproducible Test Data
import{createRNG}from'@chaisser/random-seed';functiongenerateTestUsers(count: number,seed=42){constrng=createRNG(seed);returnArray.from({length: count},(_,i)=>({id: i+1,name: rng.alpha(8),score: rng.nextInt(0,100),active: rng.nextBool(0.7)}));}// Same data every timeconstusers=generateTestUsers(100);
Shuffle a Playlist
import{createRNG}from'@chaisser/random-seed';functionshufflePlaylist(tracks: string[],seed: number){returncreateRNG(seed).shuffle(tracks);}// Same seed = same orderconstshuffled=shufflePlaylist(['song1','song2','song3','song4'],42);
import{createRNG,gaussian,intArray}from'@chaisser/random-seed';constrng=createRNG(42);// Normal distribution (mean=100, stdDev=15)constiq=gaussian(100,15,rng);// 1000 IQ scoresconstscores=gaussianArray(1000,100,15,rng);// 10 unique lottery numbersconstlottery=intArray(1,49,6,rng);