CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-seedrandom

Seeded random number generator for Javascript.

Pending
Overview
Eval results
Files

alternative-prngs.mddocs/

Alternative PRNG Algorithms

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.

Capabilities

Alea PRNG

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 });

XOR128 PRNG

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)

XORWOW PRNG

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);

XorShift7 PRNG

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);

XOR4096 PRNG

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);

Tyche-i PRNG

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 Comparison

AlgorithmPeriodPerformanceBigCrush StatusBest Use Case
alea~2^1160.9xPasses all testsGeneral purpose, fastest
xor1282^128-10.9xFails some testsSpeed-critical applications
tychei~2^1271.1xNo failuresCryptographic applications
xorwow2^192-2^321.1xFails some testsBalanced speed/quality
xor40962^4128-2^321.1xNo failuresMany parallel generators
xorshift72^256-11.3xNo failuresHigh-quality randomness needed

Performance relative to native Math.random() on Node.js v0.12.2

Common Usage Patterns

Algorithm Selection

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');

Browser Loading (Individual Algorithms)

<!-- 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>

Performance Testing

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');
});

Important Notes

  • No Auto-seeding: Alternative PRNGs require explicit seeds (cannot be null/undefined)
  • Manual Entropy: Unlike main seedrandom function, these don't automatically collect entropy
  • Same Interface: All algorithms return PRNG functions with identical method signatures
  • Platform Consistency: Results are identical across different JavaScript environments
  • State Management: All algorithms support optional state save/restore functionality

Error Handling

// 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-seeding

Install with Tessl CLI

npx tessl i tessl/npm-seedrandom

docs

alternative-prngs.md

index.md

tile.json