Registry / testing / seedrandom

seedrandom

JSON →
library2.3.1jsnpmunverified

Seedrandom is a JavaScript library that provides seeded pseudorandom number generators (PRNGs). It allows developers to create reproducible sequences of 'random' numbers, which is crucial for deterministic simulations, testing, and specific cryptographic applications (though caution is advised for security-sensitive contexts due to common misuse patterns). The current stable version is 3.0.5, released in late 2019, suggesting a mature but slow-moving maintenance cadence. Key differentiators include its ability to replace the global `Math.random` for debugging or testing purposes, and its inclusion of various PRNG algorithms (such as ARC4, Alea, xor128, Tyche-i) offering different performance characteristics and period lengths. It supports use in web browsers via script tags, as a Node.js module, and as an AMD module. Developers can instantiate independent PRNGs or opt to globally override `Math.random` with a seeded sequence.

npm install seedrandom
INSTALL
IMPORT
SIG · SEEDRANDOM
S
seedrandom
testingjavascriptv2.3.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

seedrandom
import seedrandom from 'seedrandom';
import { seedrandom } from 'seedrandom'; // Incorrectly assumes named export for main function
The primary `seedrandom` function is the default export (ESM) or the module's `module.exports` (CommonJS). Use `new seedrandom('seed')` or `seedrandom('seed', { state: true })` for an independent instance, or `seedrandom('seed')` to modify `Math.random` globally.
seedrandom (CommonJS)
const seedrandom = require('seedrandom');
const { seedrandom } = require('seedrandom'); // Incorrect, main function is default/module.exports
In CommonJS, `require('seedrandom')` returns the main PRNG function. Similar to ESM, use `new seedrandom('seed')` or `seedrandom('seed', { state: true })` for an independent instance, or `seedrandom('seed')` to modify `Math.random` globally.
Alea (Algorithm)
import Alea from 'seedrandom/lib/alea';
import { alea } from 'seedrandom'; // Alea is not a named export from the main package
Alea and other specialized PRNG algorithms are provided as separate modules under `lib/`. They must be explicitly imported from their sub-path and are typically instantiated with `new Alea('seed')`. These alternative algorithms do not include autoseeding.

Demonstrates creating independent seeded PRNG instances using the default ARC4 algorithm and the Alea algorithm. Also illustrates the critical warning regarding globally overriding `Math.random`.

import seedrandom from 'seedrandom'; // Create an independent, seeded PRNG instance const myrng = new seedrandom('my-secret-seed'); console.log('Seeded PRNG instance results (ARC4):'); console.log(myrng()); // Always the same for this seed console.log(myrng()); // Always the same subsequent value // Demonstrating other PRNGs (Alea algorithm) import Alea from 'seedrandom/lib/alea'; const aleaRng = new Alea('another-seed'); // Alea requires an explicit seed console.log('\nSeeded PRNG instance results (Alea):'); console.log(aleaRng()); console.log(aleaRng.double()); // Alea can provide 56 bits of randomness // --- WARNING: Global Math.random override example --- // This directly modifies the global Math.random, making it predictable. // This should only be used for controlled testing and must be restored. console.log('\nGlobal Math.random override (WARNING!):'); const originalMathRandom = Math.random; // Save original Math.random seedrandom('global-override-seed'); // Calling without 'new' or assignment overrides Math.random console.log(Math.random()); console.log(Math.random()); // Restore original Math.random after demonstration to prevent side effects Math.random = originalMathRandom; console.log('\nMath.random restored to original behavior.');
Debug
Known issues
breakingCalling `seedrandom('seed_string')` or `Math.seedrandom('seed_string')` (when `seedrandom` is globally loaded via a script tag) without `new` or without explicitly assigning the return value to a local variable will permanently override the global `Math.random()` function. While useful for deterministic testing, this is a critical security vulnerability if done inadvertently in a production library or security-sensitive application, making `Math.random` completely predictable.
fix
Always use `const myrng = new seedrandom('seed_string');` or `const myrng = seedrandom('seed_string', { state: true });` to create an independent, local PRNG instance. Only override `Math.random` explicitly for specific testing scenarios, and ensure it is restored immediately after use.
affects: >=1.0.0
gotchaThe faster PRNG algorithms provided within the `lib/` directory (e.g., `alea`, `xor128`, `tychei`) do not include autoseeding capabilities. If you need robust, less predictable initial seeds for these algorithms in a production environment, you must generate them separately or use the main `seedrandom` function (which can autoseed) to generate a strong seed for them.
fix
Ensure a strong, explicitly generated seed is provided when instantiating faster PRNGs, e.g., `new Alea(myStrongSeed)`.
affects: >=1.0.0
gotchaDocumentation and older examples may show `new Math.seedrandom()` for instantiation. While this works in global script contexts, for modern module-based environments (Node.js, bundlers), it's cleaner and more direct to import the `seedrandom` function directly and use `new seedrandom()` or `seedrandom('seed', { state: true })` for creating instances.
fix
For module-based JavaScript, the preferred pattern is `import seedrandom from 'seedrandom'; const rng = new seedrandom('myseed');`.
affects: >=1.0.0
Errors
Common errors & fixes
My application's 'random' numbers are predictable or always the same across runs.
A third-party library or your own code inadvertently called `seedrandom('some_seed')` without `new`, which globally overwrote `Math.random` with a deterministic sequence.
fix
Audit your codebase and all dependencies for calls to `seedrandom()` or `Math.seedrandom()` that are not explicitly creating a new instance (e.g., `new seedrandom('seed')`). If `Math.random` must be overridden for testing, ensure it is restored to its original value after the test, especially in production environments.
TypeError: Cannot read properties of undefined (reading 'double') or similar when trying to use specialized PRNGs like Alea.
This usually indicates an incorrect import or instantiation of a specific PRNG algorithm (e.g., `alea`). You might be trying to access methods on an undefined object or one not correctly initialized.
fix
Ensure you are importing the specific algorithm correctly, for example, `import Alea from 'seedrandom/lib/alea';`. Then, instantiate it as a new object: `const arng = new Alea('your-seed');`.
Upgrade
Version history
2.3.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
3 hits · last 30 days
node
2
Resources
seedrandom — npm install seedrandom · libregistry