Seeded random number generator for Javascript.
npx @tessl/cli install tessl/npm-seedrandom@3.0.0seedrandom is a comprehensive seeded random number generation library for JavaScript that enables developers to create predictable pseudorandom number sequences using string or numeric seeds. It provides multiple high-quality PRNG algorithms with different performance characteristics and statistical properties, supporting browser environments, Node.js, and AMD module systems.
npm install seedrandomvar seedrandom = require('seedrandom');For ES modules:
import seedrandom from 'seedrandom';For AMD/Require.js:
require(['seedrandom'], function(seedrandom) {
var rng = seedrandom('hello.');
console.log(rng()); // Always 0.9282578795792454
});For browser usage:
<script src="//cdnjs.cloudflare.com/ajax/libs/seedrandom/3.0.5/seedrandom.min.js"></script>
<!-- Creates Math.seedrandom global -->var seedrandom = require('seedrandom');
// Create a seeded PRNG - always produces the same sequence
var rng = seedrandom('hello.');
console.log(rng()); // Always 0.9282578795792454
console.log(rng()); // Always 0.3752569768646784
// Get different precisions
console.log(rng.quick()); // Faster 32-bit precision: 0.7316977467853576
console.log(rng.int32()); // 32-bit signed integer: 1966374204
// Auto-seeded PRNG for unpredictable results
var autoRng = seedrandom();
console.log(autoRng()); // Unpredictable
// Replace Math.random globally (for testing)
seedrandom('test-seed', { global: true });
console.log(Math.random()); // Now predictable// Create predictable PRNG (modifies Math.random)
Math.seedrandom('hello.');
console.log(Math.random()); // Always 0.9282578795792454
console.log(Math.random()); // Always 0.3752569768646784
// Create local PRNG (recommended)
var myrng = new Math.seedrandom('hello.');
console.log(myrng()); // Always 0.9282578795792454
console.log(myrng.quick()); // Always 0.7316977467853576
console.log(myrng.int32()); // Always 1966374204
// Auto-seeded PRNG
var prng = new Math.seedrandom();
console.log(prng()); // Reasonably unpredictableseedrandom is built around several key components:
Core seeded random number generator using ARC4 algorithm with automatic entropy collection and cross-platform compatibility.
/**
* Creates a seeded pseudorandom number generator
* @param {string|number|null|undefined} seed - Seed value for deterministic generation
* @param {object|boolean} options - Configuration options or legacy entropy flag
* @param {function} [callback] - Legacy callback function (use options.pass instead)
* @returns {function|*} PRNG function or result from options.pass callback
*/
function seedrandom(seed, options, callback);
// Options object
interface Options {
/** Mix in additional entropy from current state */
entropy?: boolean;
/** Replace Math.random with this PRNG */
global?: boolean;
/** Enable state save/restore functionality or restore from saved state */
state?: boolean | object;
/** Custom callback for handling prng and seed */
pass?: (prng: function, seed: string, global?: any) => any;
}
// Returned PRNG function interface
interface PRNG {
/** Returns random float [0, 1) with 53-bit mantissa precision */
(): number;
/** Returns random float [0, 1) with 32-bit precision (faster) */
quick(): number;
/** Returns random 32-bit signed integer */
int32(): number;
/** Alias for main function (53-bit precision) */
double(): number;
/** Returns internal state object (only available if state option was true) */
state?(): object;
}High-performance alternative pseudorandom number generators with different statistical properties and performance characteristics. Each algorithm provides the same interface as the main function but with different internal implementations.
/**
* Alternative PRNG algorithms - all return PRNG function with same interface
* Note: Alternative PRNGs require explicit seeds (no auto-seeding)
*/
seedrandom.alea(seed, options); // Multiply-with-carry, ~2^116 period
seedrandom.xor128(seed, options); // XOR-shift, 2^128-1 period
seedrandom.xorwow(seed, options); // XOR-shift + Weyl, 2^192-2^32 period
seedrandom.xorshift7(seed, options); // 7-shift generator, 2^256-1 period
seedrandom.xor4096(seed, options); // 4096-bit XOR-shift, 2^4128-2^32 period
seedrandom.tychei(seed, options); // ChaCha-derived, ~2^127 period/**
* Seed can be string, number, or null for auto-seeding
*/
type Seed = string | number | null;
/**
* Options for configuring PRNG behavior
*/
interface Options {
entropy?: boolean; // Mix additional entropy
global?: boolean; // Replace Math.random
state?: boolean | object; // Enable/restore state
pass?: function; // Custom result handler
}
/**
* All PRNG functions return this interface
*/
interface PRNG {
(): number; // Main random function [0, 1)
quick(): number; // 32-bit precision [0, 1)
int32(): number; // 32-bit signed integer
double(): number; // Alias for main function
state?(): object; // State management (optional)
}var seedrandom = require('seedrandom');
// Enable state functionality
var rng = seedrandom('secret-seed', { state: true });
// Generate some numbers
for (var i = 0; i < 100000; i++) rng();
// Save current state
var saved = rng.state();
// Create replica from saved state
var replica = seedrandom('', { state: saved });
// Both generators now produce identical sequences
console.log(replica() === rng()); // true// Mix additional entropy with seed
var rng = seedrandom('base-seed', { entropy: true });
// Auto-seed with maximum entropy
var autoRng = seedrandom(null, { entropy: true });// Get both PRNG and processed seed
var obj = seedrandom('my-seed', {
pass: function(prng, seed) {
return { random: prng, seed: seed };
}
});
console.log(obj.random()); // Use the PRNG
console.log(obj.seed); // Access the processed seed// DANGEROUS: Never use this pattern in production libraries
Math.seedrandom('predictable-seed');
// This makes Math.random() globally predictable!
// SAFE: Always use local PRNGs in libraries
var localRng = seedrandom('seed');
var result = localRng(); // Safe, doesn't affect global state// Alternative PRNGs require explicit seeds
try {
var rng = seedrandom.alea(); // Will throw or produce poor randomness
} catch (e) {
console.log('Alternative PRNGs require explicit seeds');
}
// Correct usage
var rng = seedrandom.alea('proper-seed');// Auto-seeding uses multiple entropy sources:
// - window.crypto (browser) or crypto.randomBytes (Node.js)
// - Current time
// - DOM state (browser only)
// - Previous seeds (accumulated entropy pool)
var autoRng = seedrandom(); // Uses all available entropy
var mixedRng = seedrandom('base-seed', { entropy: true }); // Mixes seed with entropy