{"name":"pure-rand","version":"4.1.2","description":" Pure random number generator written in TypeScript","type":"commonjs","main":"lib/pure-rand.js","exports":{".":{"require":"./lib/pure-rand.js","import":"./lib/esm/pure-rand.js","default":"./lib/esm/pure-rand.js"}},"module":"lib/esm/pure-rand.js","types":"lib/types/pure-rand.d.ts","sideEffects":false,"scripts":{"format:check":"prettier --list-different \"**/*.{js,ts}\"","format:fix":"prettier --write \"**/*.{js,ts}\"","build":"tsc && tsc -p ./tsconfig.declaration.json","build:esm":"tsc --module es2015 --outDir lib/esm --moduleResolution node && cp package.esm-template.json lib/esm/package.json","build:prod":"yarn build && yarn build:esm && node postbuild/main.cjs","build:prod-ci":"cross-env EXPECT_GITHUB_SHA=true yarn build:prod","test":"jest --config jest.config.js --coverage","build:bench:old":"tsc --target es6","build:bench:new":"tsc --target es6 --outDir lib-new/","bench":"node perf/benchmark.cjs","auto:bench":"git stash && yarn build:bench:old && git stash pop && yarn build:bench:new && yarn bench"},"repository":{"type":"git","url":"git+https://github.com/dubzzz/pure-rand.git"},"author":{"name":"Nicolas DUBIEN","email":"github@dubien.org"},"license":"MIT","bugs":{"url":"https://github.com/dubzzz/pure-rand/issues"},"homepage":"https://github.com/dubzzz/pure-rand#readme","dependencies":{},"devDependencies":{"@types/jest":"^26.0.4","@types/node":"^14.0.13","benchmark":"^2.1.4","chalk":"^4.1.0","console-table-printer":"2.7.5","cross-env":"^7.0.3","fast-check":"^2.7.0","glob":"^7.1.6","jest":"^26.1.0","microtime":"^3.0.0","prettier":"2.2.1","replace-in-file":"^6.1.0","source-map-support":"^0.5.16","ts-jest":"^26.1.2","ts-node":"^9.0.0","typescript":"^4.0.2","yargs":"^16.0.3"},"keywords":["pure random","random number generator"],"funding":{"type":"opencollective","url":"https://opencollective.com/fast-check"},"_resolved":"","_integrity":"","_from":"file:pure-rand.tgz","_id":"pure-rand@4.1.2","_nodeVersion":"14.15.1","_npmVersion":"6.14.8","dist":{"integrity":"sha512-uLzZpQWfroIqyFWmX/pl0OL2JHJdoU3dbh0dvZ25fChHFJJi56J5oQZhW6QgbT2Llwh1upki84LnTwlZvsungA==","shasum":"cbad2a3e3ea6df0a8d80d8ba204779b5679a5205","tarball":"http://registry-npm.ywwl.com/pure-rand/download/pure-rand-4.1.2.tgz","fileCount":59,"unpackedSize":56779,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgDxbcCRA9TVsSAnZWagAAD34P/1zEnHRR4wBba47S8vui\nsuJM/YjtCVlZe65m93nGUCordnqAxwGty+SvvavVPwaXyTqNpt7mUZfd8tJ4\nimxqL0+e1yEzzQw14vu2QXZDIj86XvZgZl/OdcuUJnQ315uwawgVMH+cmzi2\nwuyA4qdHaQK5KT/cyDKvp8Ye+EbmJTt08MLYKSmF8Qp8+AJYbLuAGb4WKBCo\nOUaKayesNwWpIBtiIrNFVS8sLLmUHTKiHHhmWU1T7z/Op+ofS1LrCGLCQh8s\nY6gnNGgNKLnhdUjl/GIgAGMuasdND0GU9xg/fn6h7082wYFiB2s2zzq5ck+Z\nI0SwSphToHvHnn1NZNvynIoKHWOFBVTAgpUYA+I65nnVUlMoojHs7aHfYA2t\n1XsBx9nOXGaTSiXy8vC/AD0TIE1bG+83jX/9qUBPFtdfKOeDNo7Cm8nGPvPz\nOzChsA3j3wESFiVBxGGuQKQ/4wWLklebI/uG17YkPiNlT5wVMOAi4dgEJ2Wf\nnGZPub0KqaLQn/LElI7Lm9pqbsvdGFATrxL9+oKOw1zSjmdmKZz9cjAI+4pK\nVlLSG951Hj8RBaQjhyMZkr+MQM6cXZ5AwKN3D+snCy0W9FVji/e+NBIS0CQr\nFsJIgFxVzx+T8j9KilzQUHzxeTpBw7Kw36LoYbdKoAt5V3ENw/Pm2BtGmmrr\n7j+n\r\n=Vjz9\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIAgKTDrK0QMjKRZGFbaZ7+vepltpY0OTCKi6KPvBJw6RAiEA98v5QVmpc6vmkh44C7fLS3WVdLfIMetnVa3isY4r+D0="}],"size":13766,"noattachment":false,"key":"/pure-rand/-/pure-rand-4.1.2.tgz"},"_npmUser":{"name":"ndubien","email":"npm@dubien.org"},"directories":{},"maintainers":[{"name":"ndubien","email":"npm@dubien.org"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/pure-rand_4.1.2_1611601627899_0.7045475632964793"},"_hasShrinkwrap":false,"readme":"<h1>\n  <img src=\"https://raw.githubusercontent.com/dubzzz/pure-rand/main/assets/logo.svg\" alt=\"pure-rand logo\" />\n</h1>\n\nFast Pseudorandom number generators (aka PRNG) with purity in mind!\n\n[![Build Status](https://github.com/dubzzz/pure-rand/workflows/Build%20Status/badge.svg?branch=main)](https://github.com/dubzzz/pure-rand/actions)\n[![NPM Version](https://badge.fury.io/js/pure-rand.svg)](https://badge.fury.io/js/pure-rand)\n[![Monthly Downloads](https://img.shields.io/npm/dm/pure-rand)](https://www.npmjs.com/package/pure-rand)\n\n[![Codecov](https://codecov.io/gh/dubzzz/pure-rand/branch/main/graph/badge.svg)](https://codecov.io/gh/dubzzz/pure-rand)\n[![Package Quality](https://packagequality.com/shield/pure-rand.svg)](https://packagequality.com/#?package=pure-rand)\n[![Snyk Package Quality](https://snyk.io/advisor/npm-package/pure-rand/badge.svg)](https://snyk.io/advisor/npm-package/pure-rand)\n\n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/dubzzz/pure-rand/labels/good%20first%20issue)\n[![License](https://img.shields.io/npm/l/pure-rand.svg)](https://github.com/dubzzz/pure-rand/blob/main/LICENSE)\n[![Twitter](https://img.shields.io/twitter/url/https/github.com/dubzzz/pure-rand.svg?style=social)](https://twitter.com/intent/tweet?text=Check%20out%20pure-rand%20by%20%40ndubien%20https%3A%2F%2Fgithub.com%2Fdubzzz%2Fpure-rand%20%F0%9F%91%8D)\n\n## Getting started\n\n**Install it in node via:**\n\n`npm install pure-rand` or `yarn add pure-rand`\n\n**Use it in browser by doing:**\n\n`import * as prand from 'https://unpkg.com/pure-rand/lib/esm/pure-rand.js';`\n\n## Usage\n\n**Simple usage**\n\n```javascript\nimport prand from 'pure-rand';\n\nconst seed = 42;\nconst rng = prand.xoroshiro128plus(seed);\nconst firstDiceValue = prand.unsafeUniformIntDistribution(1, 6, rng); // value in {1..6}, here: 2\nconst secondDiceValue = prand.unsafeUniformIntDistribution(1, 6, rng); // value in {1..6}, here: 4\nconst thirdDiceValue = prand.unsafeUniformIntDistribution(1, 6, rng); // value in {1..6}, here: 6\n```\n\n**Pure usage**\n\nPure means that the instance `rng` will never be altered in-place. It can be called again and again and it will always return the same value. But it will also return the next `rng`. Here is an example showing how the code above can be translated into its pure version:\n\n```javascript\nimport prand from 'pure-rand';\n\nconst seed = 42;\nconst rng1 = prand.xoroshiro128plus(seed);\nconst [firstDiceValue, rng2] = prand.uniformIntDistribution(1, 6, rng1); // value in {1..6}, here: 2\nconst [secondDiceValue, rng3] = prand.uniformIntDistribution(1, 6, rng2); // value in {1..6}, here: 4\nconst [thirdDiceValue, rng4] = prand.uniformIntDistribution(1, 6, rng3); // value in {1..6}, here: 6\n\n// You can call: prand.uniformIntDistribution(1, 6, rng1);\n// over and over it will always give you back the same value along with a new rng (always producing the same values too).\n```\n\n**Independent simulations**\n\nIn order to produce independent simulations it can be tempting to instanciate several PRNG based on totally different seeds. While it would produce distinct set of values, the best way to ensure fully unrelated sequences is rather to use jumps. Jump just consists into moving far away from the current position in the generator (eg.: jumping in Xoroshiro 128+ will move you 2<sup>64</sup> generations away from the current one on a generator having a sequence of 2<sup>128</sup> elements).\n\n```javascript\nimport prand from 'pure-rand';\n\nconst seed = 42;\nconst rngSimulation1 = prand.xoroshiro128plus(seed);\nconst rngSimulation2 = rngSimulation1.jump(); // not in-place, creates a new instance\nconst rngSimulation3 = rngSimulation2.jump(); // not in-place, creates a new instance\n\nconst diceSim1Value = prand.unsafeUniformIntDistribution(1, 6, rngSimulation1); // value in {1..6}, here: 2\nconst diceSim2Value = prand.unsafeUniformIntDistribution(1, 6, rngSimulation2); // value in {1..6}, here: 5\nconst diceSim3Value = prand.unsafeUniformIntDistribution(1, 6, rngSimulation3); // value in {1..6}, here: 6\n```\n\n**Non-uniform usage**\n\nWhile not recommended as non-uniform distribution implies that one or several values from the range will be more likely than others, it might be tempting for people wanting to maximize the throughput.\n\n```javascript\nimport prand from 'pure-rand';\n\nconst seed = 42;\nconst rng = prand.xoroshiro128plus(seed);\nconst rand = (min, max) => {\n  const out = (rng.unsafeNext() >>> 0) / 0x100000000;\n  return min + Math.floor(out * (max - min + 1));\n};\nconst firstDiceValue = rand(1, 6); // value in {1..6}, here: 6\n```\n\n**Select your seed**\n\nWhile not perfect, here is a rather simple way to generate a seed for your PNRG.\n\n```javascript\nconst seed = Date.now() ^ (Math.random() * 0x100000000);\n```\n\n## Documentation\n\n### Pseudorandom number generators\n\nIn computer science most random number generators<sup>(1)</sup> are [pseudorandom number generators](https://en.wikipedia.org/wiki/Pseudorandom_number_generator) (abbreviated: PRNG). In other words, they are fully deterministic and given the original seed one can rebuild the whole sequence.\n\nEach PRNG algorithm has to deal with tradeoffs in terms of randomness quality, speed, length of the sequence<sup>(2)</sup>... In other words, it's important to compare relative speed of libraries with that in mind. Indeed, a Mersenne Twister PRNG will not have the same strenghts and weaknesses as a Xoroshiro PRNG, so depending on what you need exactly you might prefer one PRNG over another even if it will be slower.\n\n4 PRNGs come with pure-rand:\n\n- `congruential32`: Linear Congruential generator — \\[[more](https://en.wikipedia.org/wiki/Linear_congruential_generator)\\]\n- `mersenne`: Mersenne Twister generator — \\[[more](https://en.wikipedia.org/wiki/Mersenne_Twister)\\]\n- `xorshift128plus`: Xorshift 128+ generator — \\[[more](https://en.wikipedia.org/wiki/Xorshift)\\]\n- `xoroshiro128plus`: Xoroshiro 128+ generator — \\[[more](https://en.wikipedia.org/wiki/Xorshift)\\]\n\nOur recommendation is `xoroshiro128plus`. But if you want to use another one, you can replace it by any other PRNG provided by pure-rand in the examples above.\n\n### Distributions\n\nOnce you are able to generate random values, next step is to scale them into the range you want. Indeed, you probably don't want a floating point value between 0 (included) and 1 (excluded) but rather an integer value between 1 and 6 if you emulate a dice or any other range based on your needs.\n\nAt this point, simple way would be to do `min + floor(random() * (max - min + 1))` but actually it will not generate the values with equal probabilities even if you use the best PRNG in the world to back `random()`. In order to have equal probabilities you need to rely on uniform distributions<sup>(3)</sup> which comes built-in in some PNRG libraries.\n\npure-rand provides 3 built-in functions for uniform distributions of values:\n\n- `uniformIntDistribution(min, max, rng)`\n- `uniformBigIntDistribution(min, max, rng)` - with `min` and `max` being `bigint`\n- `uniformArrayIntDistribution(min, max, rng)` - with `min` and `max` being instances of `ArrayInt = {sign, data}` ie. sign either 1 or -1 and data an array of numbers between 0 (included) and 0xffffffff (included)\n\nAnd their unsafe equivalents to change the PRNG in-place.\n\n### Extra helpers\n\nSome helpers are also provided in order to ease the use of `RandomGenerator` instances:\n\n- `prand.generateN(rng: RandomGenerator, num: number): [number[], RandomGenerator]`: generates `num` random values using `rng` and return the next `RandomGenerator`\n- `prand.skipN(rng: RandomGenerator, num: number): RandomGenerator`: skips `num` random values and return the next `RandomGenerator`\n\n## Comparison\n\n### Summary\n\nThe chart has been split into three sections:\n\n- section 1: native `Math.random()`\n- section 2: without uniform distribution of values\n- section 3: with uniform distribution of values (not supported by all libraries)\n\n<img src=\"https://raw.githubusercontent.com/dubzzz/pure-rand/main/perf/comparison.svg\" alt=\"Comparison against other libraries\" />\n\n### Process\n\nIn order to compare the performance of the libraries, we aked them to shuffle an array containing 1,000,000 items (see [code](https://github.com/dubzzz/pure-rand/blob/556ec331c68091c5d56e9da1266112e8ea222b2e/perf/compare.cjs)).\n\nWe then split the measurements into two sections:\n\n- one for non-uniform distributions — _known to be slower as it implies re-asking for other values to the PRNG until the produced value fall into the acceptable range of values_\n- one for uniform distributions\n\nThe recommended setup for pure-rand is to rely on our Xoroshiro128+. It provides a long enough sequence of random values, has built-in support for jump, is really efficient while providing a very good quality of randomness.\n\n### Performance\n\n**Non-Uniform**\n\n| Library                  | Algorithm         | Mean time (ms) | Compared to pure-rand |\n| ------------------------ | ----------------- | -------------- | --------------------- |\n| native \\(node 16.19.1\\)  | Xorshift128+      | 33.3           | 1.4x slower           |\n| **pure-rand _@6.0.0_**   | **Xoroshiro128+** | **24.5**       | **reference**         |\n| pure-rand _@6.0.0_       | Xorshift128+      | 25.0           | similar               |\n| pure-rand _@6.0.0_       | Mersenne Twister  | 30.8           | 1.3x slower           |\n| pure-rand _@6.0.0_       | Congruential‍     | 22.6           | 1.1x faster           |\n| seedrandom _@3.0.5_      | Alea              | 28.1           | 1.1x slower           |\n| seedrandom _@3.0.5_      | Xorshift128       | 28.8           | 1.2x slower           |\n| seedrandom _@3.0.5_      | Tyche-i           | 28.6           | 1.2x slower           |\n| seedrandom _@3.0.5_      | Xorwow            | 32.0           | 1.3x slower           |\n| seedrandom _@3.0.5_      | Xor4096           | 32.2           | 1.3x slower           |\n| seedrandom _@3.0.5_      | Xorshift7         | 33.5           | 1.4x slower           |\n| @faker-js/faker _@7.6.0_ | Mersenne Twister  | 109.1          | 4.5x slower           |\n| chance _@1.1.10_         | Mersenne Twister  | 142.9          | 5.8x slower           |\n\n**Uniform**\n\n| Library                | Algorithm         | Mean time (ms) | Compared to pure-rand |\n| ---------------------- | ----------------- | -------------- | --------------------- |\n| **pure-rand _@6.0.0_** | **Xoroshiro128+** | **53.5**       | **reference**         |\n| pure-rand _@6.0.0_     | Xorshift128+      | 52.2           | similar               |\n| pure-rand _@6.0.0_     | Mersenne Twister  | 61.6           | 1.2x slower           |\n| pure-rand _@6.0.0_     | Congruential‍     | 57.6           | 1.1x slower           |\n| random-js @2.1.0       | Mersenne Twister  | 119.6          | 2.2x slower           |\n\n> System details:\n>\n> - OS: Linux 5.15 Ubuntu 22.04.2 LTS 22.04.2 LTS (Jammy Jellyfish)\n> - CPU: (2) x64 Intel(R) Xeon(R) Platinum 8272CL CPU @ 2.60GHz\n> - Memory: 5.88 GB / 6.78 GB\n> - Container: Yes\n> - Node: 16.19.1 - /opt/hostedtoolcache/node/16.19.1/x64/bin/node\n>\n> _Executed on default runners provided by GitHub Actions_\n\n---\n\n(1) — Not all as there are also [hardware-based random number generator](https://en.wikipedia.org/wiki/Hardware_random_number_generator).\n\n(2) — How long it takes to reapeat itself?\n\n(3) — While most users don't really think of it, uniform distribution is key! Without it entries might be biased towards some values and make some others less probable. The naive `rand() % numValues` is a good example of biased version as if `rand()` is uniform in `0, 1, 2` and `numValues` is `2`, the probabilities are: `P(0) = 67%`, `P(1) = 33%` causing `1` to be less probable than `0`\n","publish_time":1611601628030,"_cnpm_publish_time":1611601628030,"dist-tags":{"experimental":"5.0.0-experimental.4.2.1.0","latest":"8.4.2","next":"3.0.0-alpha.2.0.0.2"}}