An open API service indexing awesome lists of open source software.

https://github.com/kripod/pcg.js

A functional implementation of the PCG family random number generators, written in JavaScript.
https://github.com/kripod/pcg.js

Last synced: 12 months ago
JSON representation

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

Awesome Lists containing this project

README

          

# pcg.js

[![NPM](https://img.shields.io/npm/v/pcg.svg)](https://www.npmjs.com/package/pcg)
[![Travis CI](https://img.shields.io/travis/kripod/pcg.js/master.svg)](https://travis-ci.org/kripod/pcg.js)
[![Codecov](https://img.shields.io/codecov/c/github/kripod/pcg.js.svg)](https://codecov.io/gh/kripod/pcg.js)

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

[PCG family random number generators]: http://pcg-random.org

## Getting started

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

Firstly, create a random number engine:

```js
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:

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