esm-seedrandom
Explicitly seeded random number generator for JavaScript, ported to ES Modules.
Unit tested for number generator compatability with original seedrandom CommonJS NPM package.
- Version: 3.0.5
- Author: David Bau
- Date: 2019-09-14
- ES Modules port in 2020-12 by Shane Holloway
Demo
GitHub Pages-based Live demo
Use
Fast PRNG Algorithms
To use Johannes Baagøe's extremely fast Alea PRNG:
import {prng_alea} from 'esm-seedrandom';
let myrng = prng_alea('hello.');
console.log(myrng());
console.log(myrng.quick());
console.log(myrng.double());
console.log(myrng.int32());
or direclty from HTML,
<script type="module">
import {prng_alea} from '//cdn.jsdelivr.net/npm/esm-seedrandom/esm/alea.min.mjs'
let myrng = prng_alea('an example seed string')
console.log(myrng());
console.log(myrng());
console.log(myrng());
console.log(myrng.quick());
console.log(myrng.quick());
console.log(myrng.int32());
console.log(myrng.int32());
console.log(myrng.double());
console.log(myrng.double());
</script>
Docs
See the API docs.
Overview
From NodeJS,
npm install esm-seedrandom
or in HTML,
<script type='module'>
import {prng_alea} from '//cdn.jsdelivr.net/npm/esm-seedrandom/esm/index.min.mjs'
import {prng_alea} from '//cdn.jsdelivr.net/npm/esm-seedrandom/esm/alea.min.mjs'
import {prng_xor128} from '//cdn.jsdelivr.net/npm/esm-seedrandom/esm/xor128.min.mjs'
import {prng_tychei} from '//cdn.jsdelivr.net/npm/esm-seedrandom/esm/tychei.min.mjs'
import {prng_xorwow} from '//cdn.jsdelivr.net/npm/esm-seedrandom/esm/xorwow.min.mjs'
import {prng_xor4096} from '//cdn.jsdelivr.net/npm/esm-seedrandom/esm/xor4096.min.mjs'
import {prng_xorshift7} from '//cdn.jsdelivr.net/npm/esm-seedrandom/esm/xorshift7.min.mjs'
import {prng_arc4} from '//cdn.jsdelivr.net/npm/esm-seedrandom/esm/arc4.min.mjs'
</script>
Saving & Restoring PRNG state
import {prng_alea} from 'esm-seedrandom';
let rng_first = prng_alea("secret-seed", {state: true});
let saved_state = rng_first.state()
for (let j = 0; j < 1e5; ++j)
rng_first();
let rng_replica = prng_alea("", {state: saved_state});
for (let j = 0; j < 1e5; ++j)
rng_replica();
In normal use the prng is opaque and its internal state cannot be accessed.
However, if the state
option is provided, the prng gets a state() method
that returns a plain object the can be used to reconstruct a prng later in
the same state (by passing that saved object back as the state option).
Unit tests
Mocha-based live unittests
for reproducability and validation of state capture and restore.
In NodeJS unittests, validation of state snapshot compatability with the original seedrandom CommonJS implementation is performed.
LICENSE (MIT)
Copyright 2020 Shane Holloway. (ES Module port)
Copyright 2019 David Bau.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.