seedrandom

repository·released·Indexed 24 days ago

https://github.com/davidbau/seedrandom

A seeded pseudorandom number generator (PRNG) for JavaScript version 3.0.5. It allows for predictable sequences of random numbers via seeds, which is useful for testing, procedural generation, and reproducible simulations. It supports creating local PRNG instances, replacing the global Math.random, and provides multiple alternative algorithms including alea, xor128, tychei, xorwow, xor4096, and xorshift7.

Tokens
1.5K
Snippets
9
Records
11
Agent score
30%

What's inside seedrandom

  1. Replace global Math.random with a predictable generator

    released

    Warning: Calling Math.seedrandom(seed) without the new keyword replaces the global Math.random() function with a predictable version. This is useful for derandomizing code during testing, but should be avoided in production libraries as it makes Math.random() predictable for all code in the environment.

    Math.seedrandom('hello.');
    console.log(Math.random());          // Always 0.9282578795792454
  2. Use seedrandom via Script Tag

    released

    To use seedrandom in a browser without a module loader, include the script tag in your HTML. This will attach seedrandom to the Math object.

    <script src="//cdnjs.cloudflare.com/ajax/libs/seedrandom/3.0.5/seedrandom.min.js"></script>
  3. Use seedrandom in Node.js (CJS)

    released

    In Node.js, require('seedrandom') returns a function. Note that in version 3+, the global Math.seedrandom is no longer available when using this method.

    • Local PRNG: var rng = seedrandom('seed'); (does not affect Math.random)
    • Global PRNG: seedrandom('seed', { global: true }); (sets Math.random)
    • Autoseeded: var rng = seedrandom(); (uses entropy for unpredictability)
    • Mixed Entropy: seedrandom('seed', { entropy: true }); (mixes seed with accumulated entropy)
    var seedrandom = require('seedrandom');
    var rng = seedrandom('hello.');
    console.log(rng());
    
    // To set global Math.random:
    seedrandom('hello.', { global: true });
  4. Create a local seeded PRNG

    released

    To create a predictable pseudorandom number generator without affecting the global Math.random(), use the new keyword with Math.seedrandom(seed). This is recommended for production libraries to avoid side effects.

    Methods available on the returned generator:

    • myrng(): Returns a pseudorandom float.
    • myrng.quick(): Returns 32 bits of randomness in a float.
    • myrng.int32(): Returns a 32-bit signed integer.
    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
  5. Get both PRNG and seed using the 'pass' option

    released

    You can use the { pass: callback } option to retrieve both the generated PRNG and the seed used to create it without mutating the global Math.random.

    var obj = Math.seedrandom(null, { pass: function(prng, seed) {
      return { random: prng, seed: seed };
    }});
  6. Save and restore PRNG state

    released

    By default, the PRNG is opaque. However, if you initialize it with the { state: true } option, it gains a .state() method. This method returns a plain object representing the internal state, which can be passed back into a new seedrandom instance via the { state: savedState } option to reconstruct the exact same sequence.

    var seedrandom = Math.seedrandom;
    var saveable = seedrandom("secret-seed", {state: true});
    for (var j = 0; j < 1e5; ++j) saveable();
    var saved = saveable.state();
    
    // Restore state later
    var replica = seedrandom("", {state: saved});
    // replica() will now produce the same sequence as saveable()
  7. Use alternate fast PRNG algorithms

    released

    Besides the standard ARC4-based generator, seedrandom provides access to several faster algorithms. In Node.js, these are accessed via properties on the required module (e.g., seedrandom.xor4096('seed')).

    Available Algorithms:

    • alea: Extremely fast floating-point RNG (requires separate script in browser).
    • xor128
    • tychei
    • xorwow
    • xor4096
    • xorshift7
    • quick: 32-bit version of the ARC4-based PRNG.
    var seedrandom = require('seedrandom');
    var rng2 = seedrandom.xor4096('hello.');
    console.log(rng2());
  8. Initialize a seeded PRNG with seedrandom()

    released

    To create a seeded pseudo-random number generator (PRNG), require seedrandom and call it as a function with your desired seed. The returned function can be called to generate random numbers. By default, it uses an ARC4-based generator with a period of approximately 2^1600.

    var seedrandom = require('seedrandom');
    var random = seedrandom(1); // or any seed.
    var x = random();           // 0 <= x < 1. Every bit is random.
  9. Access alternative PRNG algorithms

    released
    The seedrandom export object provides access to several alternative PRNG algorithms that you can use. These algorithms vary in period, complexity, and performance characteristics (e.g., passing BigCrush tests).
  10. Generate fast 32-bit random numbers with .quick()

    released

    The PRNG instance returned by seedrandom() includes a .quick() method. Calling random.quick() returns a value where 0 <= x < 1, providing 32 bits of randomness. This is intended for use cases where speed is prioritized over full bit-level randomness.

    var seedrandom = require('seedrandom');
    var random = seedrandom(1);
    var x = random.quick(); // 0 <= x < 1. 32 bits of randomness.