Skip to content

jurerotar/ts-seedrandom

Repository files navigation

ts-seedrandom

Seeded random number generators for JavaScript, ported to TypeScript. Based on https://github.com/shanewholloway/js-esm-seedrandom.

Installation

npm install ts-seedrandom

Usage

Each generator includes the following methods:

  • quick - Default method used. Provides 32 bits of randomness in a float. Can either be called by calling generator instance directly (ex. generator()) or by name (ex. generator.quick()).
  • double - Provides 56 bits of randomness.
  • int32 - Providers a 32 bit (signed) integer.
  • state - Provides internal generator state. Used for saving and restoring states.
import { prngAlea } from 'ts-seedrandom';

const aleaGenerator = prngAlea('seed');
const firstValue = aleaGenerator();
const secondValue = aleaGenerator();

You also have the option of saving and restoring state of your generator.

import { prngAlea } from 'ts-seedrandom';

const aleaGenerator = prngAlea('seed');
const firstValue = aleaGenerator();

// Return internal generator state, which you can use in other generator instances
const state = aleaGenerator.state();

// This generator starts from the same state as first generator, but runs independently
const secondAleaGenerator = prngAlea('seed', state);

Available Algorithms

The following PRNG algorithms are available:

  1. prngAlea: Alea algorithm
  2. prngArc4: ARC4 algorithm
  3. prngTychei: Tyche-i algorithm
  4. prngXor128: XorShift128 algorithm
  5. prngXor4096: XorShift4096 algorithm
  6. prngXorshift7: XorShift7 algorithm
  7. prngXorwow: Xorwow algorithm

You can import and use any of these algorithms in the same way as demonstrated in the usage examples above.