{"id":11992,"library":"seedrandom","title":"Seeded Random Number Generator","description":"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.","status":"maintenance","version":"3.0.5","language":"javascript","source_language":"en","source_url":"git://github.com/davidbau/seedrandom","tags":["javascript","seed","random","crypto"],"install":[{"cmd":"npm install seedrandom","lang":"bash","label":"npm"},{"cmd":"yarn add seedrandom","lang":"bash","label":"yarn"},{"cmd":"pnpm add seedrandom","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"import { seedrandom } from 'seedrandom'; // Incorrectly assumes named export for main function","symbol":"seedrandom","correct":"import seedrandom from 'seedrandom';"},{"note":"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.","wrong":"const { seedrandom } = require('seedrandom'); // Incorrect, main function is default/module.exports","symbol":"seedrandom (CommonJS)","correct":"const seedrandom = require('seedrandom');"},{"note":"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.","wrong":"import { alea } from 'seedrandom'; // Alea is not a named export from the main package","symbol":"Alea (Algorithm)","correct":"import Alea from 'seedrandom/lib/alea';"}],"quickstart":{"code":"import seedrandom from 'seedrandom';\n\n// Create an independent, seeded PRNG instance\nconst myrng = new seedrandom('my-secret-seed');\nconsole.log('Seeded PRNG instance results (ARC4):');\nconsole.log(myrng()); // Always the same for this seed\nconsole.log(myrng()); // Always the same subsequent value\n\n// Demonstrating other PRNGs (Alea algorithm)\nimport Alea from 'seedrandom/lib/alea';\nconst aleaRng = new Alea('another-seed'); // Alea requires an explicit seed\nconsole.log('\\nSeeded PRNG instance results (Alea):');\nconsole.log(aleaRng());\nconsole.log(aleaRng.double()); // Alea can provide 56 bits of randomness\n\n// --- WARNING: Global Math.random override example ---\n// This directly modifies the global Math.random, making it predictable.\n// This should only be used for controlled testing and must be restored.\nconsole.log('\\nGlobal Math.random override (WARNING!):');\nconst originalMathRandom = Math.random; // Save original Math.random\nseedrandom('global-override-seed'); // Calling without 'new' or assignment overrides Math.random\nconsole.log(Math.random());\nconsole.log(Math.random());\n\n// Restore original Math.random after demonstration to prevent side effects\nMath.random = originalMathRandom;\nconsole.log('\\nMath.random restored to original behavior.');","lang":"javascript","description":"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`."},"warnings":[{"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.","message":"Calling `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.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Ensure a strong, explicitly generated seed is provided when instantiating faster PRNGs, e.g., `new Alea(myStrongSeed)`.","message":"The 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For module-based JavaScript, the preferred pattern is `import seedrandom from 'seedrandom'; const rng = new seedrandom('myseed');`.","message":"Documentation 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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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.","cause":"A third-party library or your own code inadvertently called `seedrandom('some_seed')` without `new`, which globally overwrote `Math.random` with a deterministic sequence.","error":"My application's 'random' numbers are predictable or always the same across runs."},{"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');`.","cause":"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.","error":"TypeError: Cannot read properties of undefined (reading 'double') or similar when trying to use specialized PRNGs like Alea."}],"ecosystem":"npm"}