kripod / pcg.js

A functional implementation of the PCG family random number generators, written in JavaScript.
MIT License
1 stars 1 forks source link

pcg.js

NPM Travis CI Codecov

A functional implementation of the PCG family random number generators(), written in JavaScript.

Getting started

To achieve frictionless reproducibility of random output results, immutable objects are used throughout the project.

Firstly, create a random number engine:

import { createPcg32 } from 'pcg';

const advancedOptions = {};
const initState = 42;
const initStreamId = 54;
const pcg = createPcg32(advancedOptions, initState, initStreamId);

After that, random outputs can be generated by calling appropriate functions as shown below:

import { nextState, prevState, randomInt, randomList } from 'pcg';

const randomUint32 = randomInt(0, (2 ** 32) - 1);
let [value, nextPcg] = randomUint32(pcg);

const listLength = 6;
const listItemRng = randomUint32;
[value, nextPcg] = randomList(listLength, listItemRng, pcg);