Class: Generator

Generator(bitGeneratoropt)

A random number generator with its own independent stream, like numpy's np.random.Generator. Every sampling function of the library is available as a method with the same signature (rng.normal(0, 1, 10), rng.choice(...), rng.dirichlet(...) ...), and does not touch the global generator controlled by seed().

Constructor

new Generator(bitGeneratoropt)

Parameters:
Name Type Attributes Description
bitGenerator BitGenerator <optional>

The underlying bit generator. A randomly seeded xoshiro128** is used when omitted.

Source:
Example
const rng = createGenerator(42)
rng.normal(0, 1, 5)
rng.integers(0, 10, [2, 3])
const saved = rng.getState()
const a = rng.random()
rng.setState(saved)
rng.random() === a // true

Classes

Generator

Members

bitGenerator :BitGenerator

The underlying bit generator.

Type:
  • BitGenerator
Source:

Methods

clone() → {Generator}

Returns an independent copy positioned at the same point of the stream.

Source:
Returns:
Type
Generator

getState() → {Object}

Returns a JSON serialisable snapshot of the generator state.

Source:
Returns:
Type
Object

run(fn) → {*}

Runs fn with this generator installed as the source of every module level sampling function, so third party code built on the global functions uses this stream.

Parameters:
Name Type Description
fn function
Source:
Returns:

The value returned by fn.

Type
*

setState(state) → {this}

Restores a snapshot returned by Generator#getState.

Parameters:
Name Type Description
state Object
Source:
Returns:
Type
this

spawn(n) → {Array.<Generator>}

Creates n child generators with statistically independent streams, seeded from this generator (deterministic when this generator is seeded). Useful for parallel simulations.

Parameters:
Name Type Description
n number

Number of children.

Source:
Returns:
Type
Array.<Generator>