Seeded random number generator for Javascript.
—
seedrandom provides 6 high-performance alternative pseudorandom number generators, each with different statistical properties and performance characteristics. These algorithms are faster than the main ARC4-based generator but do not provide automatic entropy collection.
Johannes Baagøe's multiply-with-carry generator with excellent statistical properties and fast performance.
/**
* Creates Alea PRNG - multiply-with-carry algorithm
* Period: ~2^116, passes all BigCrush tests
* Performance: ~1.95ns per call (0.9x native Math.random)
* @param {string|number} seed - Seed value (required, no auto-seeding)
* @param {object} options - Configuration options
* @returns {function} PRNG function with standard interface
*/
seedrandom.alea(seed, options);Usage Example:
var seedrandom = require('seedrandom');
// Create Alea PRNG
var rng = seedrandom.alea('hello.');
console.log(rng()); // 0.4783254903741181 (32-bit precision for alea)
console.log(rng.double()); // Higher precision: 0.8297006866124559
console.log(rng.int32()); // 32-bit integer: 1076136327
// With state management
var statefulRng = seedrandom.alea('test', { state: true });
var saved = statefulRng.state();
var replica = seedrandom.alea('', { state: saved });George Marsaglia's XOR-shift generator - simple and fast with good statistical properties.
/**
* Creates XOR128 PRNG - pure XOR-shift algorithm
* Period: 2^128-1
* Performance: ~2.04ns per call (0.9x native Math.random)
* Known issues: Fails MatrixRank and LinearComp tests
* @param {string|number} seed - Seed value (required)
* @param {object} options - Configuration options
* @returns {function} PRNG function with standard interface
*/
seedrandom.xor128(seed, options);Usage Example:
var rng = seedrandom.xor128(12345);
console.log(rng()); // Fast random number (32-bit precision)
console.log(rng.quick()); // Same as main function for xor128
console.log(rng.int32()); // 32-bit integer output
console.log(rng.double()); // Higher precision (53-bit)Marsaglia's 160-bit XOR-shift combined with Weyl generator for improved randomness.
/**
* Creates XORWOW PRNG - XOR-shift combined plus Weyl
* Period: 2^192-2^32
* Performance: ~2.40ns per call (1.1x native Math.random)
* Known issues: Fails CollisionOver, SimpPoker, and LinearComp tests
* @param {string|number} seed - Seed value (required)
* @param {object} options - Configuration options
* @returns {function} PRNG function with standard interface
*/
seedrandom.xorwow(seed, options);Panneton and L'ecuyer's 7-shift generator with enhanced robustness through additional shifts.
/**
* Creates XorShift7 PRNG - 7-shift generator with 256 bits
* Period: 2^256-1
* Performance: ~2.64ns per call (1.3x native Math.random)
* Quality: Passes BigCrush with no systematic failures
* @param {string|number} seed - Seed value (required)
* @param {object} options - Configuration options
* @returns {function} PRNG function with standard interface
*/
seedrandom.xorshift7(seed, options);Richard Brent's 4096-bit XOR-shift with Weyl generator for extremely long periods.
/**
* Creates XOR4096 PRNG - 4096-bit XOR-shift with Weyl
* Period: 2^4128-2^32 (extremely long period)
* Performance: ~2.40ns per call (1.1x native Math.random)
* Quality: Passes BigCrush with no systematic failures
* Use case: Many generators needing to avoid collisions
* @param {string|number} seed - Seed value (required)
* @param {object} options - Configuration options
* @returns {function} PRNG function with standard interface
*/
seedrandom.xor4096(seed, options);Neves and Araujo's bit-shifting generator derived from the ChaCha stream cipher.
/**
* Creates Tyche-i PRNG - ChaCha-derived bit-shifting generator
* Period: ~2^127
* Performance: ~2.32ns per call (1.1x native Math.random)
* Quality: Passes BigCrush with no systematic failures
* @param {string|number} seed - Seed value (required)
* @param {object} options - Configuration options
* @returns {function} PRNG function with standard interface
*/
seedrandom.tychei(seed, options);| Algorithm | Period | Performance | BigCrush Status | Best Use Case |
|---|---|---|---|---|
| alea | ~2^116 | 0.9x | Passes all tests | General purpose, fastest |
| xor128 | 2^128-1 | 0.9x | Fails some tests | Speed-critical applications |
| tychei | ~2^127 | 1.1x | No failures | Cryptographic applications |
| xorwow | 2^192-2^32 | 1.1x | Fails some tests | Balanced speed/quality |
| xor4096 | 2^4128-2^32 | 1.1x | No failures | Many parallel generators |
| xorshift7 | 2^256-1 | 1.3x | No failures | High-quality randomness needed |
Performance relative to native Math.random() on Node.js v0.12.2
var seedrandom = require('seedrandom');
// For fastest performance
var fast = seedrandom.alea('speed-test');
// For cryptographic quality
var crypto = seedrandom.tychei('crypto-seed');
// For many parallel generators
var parallel1 = seedrandom.xor4096('seed-1');
var parallel2 = seedrandom.xor4096('seed-2');
// Extremely unlikely to have overlapping sequences
// For highest quality randomness
var quality = seedrandom.xorshift7('quality-seed');<!-- Load individual algorithm -->
<script src="//cdnjs.cloudflare.com/ajax/libs/seedrandom/3.0.5/lib/alea.min.js"></script>
<script>
var rng = alea('browser-seed');
console.log(rng()); // Uses Alea algorithm directly
</script>
<!-- Multiple algorithms -->
<script src="//cdnjs.cloudflare.com/ajax/libs/seedrandom/3.0.5/lib/alea.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/seedrandom/3.0.5/lib/xor128.min.js"></script>
<script>
var aleaRng = alea('seed1');
var xorRng = xor128('seed2');
</script>var seedrandom = require('seedrandom');
// Performance comparison
var algorithms = [
{ name: 'alea', fn: seedrandom.alea('test') },
{ name: 'xor128', fn: seedrandom.xor128('test') },
{ name: 'tychei', fn: seedrandom.tychei('test') },
{ name: 'native', fn: Math.random }
];
algorithms.forEach(function(alg) {
var start = Date.now();
for (var i = 0; i < 1000000; i++) {
alg.fn();
}
var time = Date.now() - start;
console.log(alg.name + ': ' + time + 'ms');
});// These will throw errors or produce poor randomness:
try {
var badRng = seedrandom.alea(); // No seed provided
} catch (e) {
console.log('Seed required for alternative PRNGs');
}
// Proper usage:
var goodRng = seedrandom.alea('proper-seed');
var autoSeedRng = seedrandom(); // Main function handles auto-seedingInstall with Tessl CLI
npx tessl i tessl/npm-seedrandom