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.
- Host: GitHub
- URL: https://github.com/kripod/pcg.js
- Owner: kripod
- License: mit
- Created: 2017-01-30T18:01:10.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-02-06T10:06:57.000Z (over 3 years ago)
- Last Synced: 2025-03-30T17:22:09.735Z (about 1 year ago)
- Language: JavaScript
- Size: 39.1 KB
- Stars: 1
- Watchers: 4
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pcg.js
[](https://www.npmjs.com/package/pcg)
[](https://travis-ci.org/kripod/pcg.js)
[](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);
```