alea-generator
Version:
A performant and effective random number generator
77 lines β’ 8.84 kB
JSON
{
"name": "alea-generator",
"repository": {
"type": "git",
"url": "git+https://github.com/illumincrotty/alea-random.git"
},
"homepage": "https://github.com/illumincrotty/alea-random#readme",
"bugs": {
"url": "https://github.com/illumincrotty/alea-random/issues"
},
"version": "1.0.0",
"description": "A performant and effective random number generator",
"type": "module",
"files": [
"dist",
"src"
],
"source": "src/alea.ts",
"exports": {
"require": "./dist/alea.cjs",
"default": "./dist/alea.modern.js"
},
"main": "./dist/alea.cjs",
"module": "./dist/alea.js",
"unpkg": "./dist/alea.umd.js",
"types": "./dist/alea.d.ts",
"devDependencies": {
"@skypack/package-check": "^0.2.2",
"@typescript-eslint/eslint-plugin": "^5.20.0",
"@typescript-eslint/parser": "^5.20.0",
"ava": "^4.2.0",
"c8": "^7.11.2",
"eslint": "^8.13.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-ava": "^13.2.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-unicorn": "^42.0.0",
"microbundle": "^0.14.2",
"prettier": "^2.6.2",
"ts-node": "^10.7.0",
"typescript": "^4.6.3"
},
"author": "Brian Crotty",
"license": "MIT",
"keywords": [
"random",
"integer",
"generator",
"alea"
],
"ava": {
"files": [
"test/*.test.ts",
"test/**/*.test.ts"
],
"failFast": false,
"extensions": {
"ts": "module"
},
"nodeArguments": [
"--loader=ts-node/esm",
"--experimental-specifier-resolution=node",
"--no-warnings=ExperimentalWarning"
]
},
"mangle": {
"regex": "^_"
},
"scripts": {
"test": "ava",
"build": "microbundle",
"dev": "microbundle watch",
"ci": "c8 --reporter lcovonly pnpm test",
"coverage": "c8 --reporter lcov --reporter text --skip-full pnpm test"
},
"readme": "# Alea Generator\r\n\r\n<!-- Shields -->\r\n\r\n\r\n\r\n\r\n[](https://codecov.io/gh/illumincrotty/alea-random)\r\n\r\n</div>\r\n\r\n---\r\n\r\n<p align=\"center\"> A dead simple random number generator. Seedable, repeatable, and fast</p>\r\n\r\n## π Table of Contents\r\n\r\n- [Alea Generator](#alea-generator)\r\n - [π Table of Contents](#-table-of-contents)\r\n - [π§ About](#-about)\r\n - [Install](#install)\r\n - [Package Manager](#package-manager)\r\n - [CDN](#cdn)\r\n - [π§ Running the tests](#-running-the-tests)\r\n - [π Usage](#-usage)\r\n - [π License and Shoutouts](#-license-and-shoutouts)\r\n - [βοΈ Authors](#οΈ-authors)\r\n - [π¨ Similar Tools](#-similar-tools)\r\n\r\n## π§ About\r\n\r\nThe default javascript random (Math.random()) is slow, inconsistent across browsers and platforms, and lack seedability, customizability, and state. This generator is a simple way to solve all these problems at once, and it manages that at the tiny cost of only a few hundred bytes. Not kilobytes, bytes. It's small, it's fast, and it's feature-rich. What more could you want?\r\n\r\n## Install\r\n\r\n### Package Manager\r\n\r\n#### NPM <!-- omit in TOC -->\r\n\r\n```sh\r\nnpm i alea-generator\r\n```\r\n\r\n#### PNPM <!-- omit in TOC -->\r\n\r\n```sh\r\npnpm add alea-generator\r\n```\r\n\r\n#### Yarn <!-- omit in TOC -->\r\n\r\n```sh\r\nyarn add alea-generator\r\n```\r\n\r\n### CDN\r\n\r\n#### Skypack <!-- omit in TOC -->\r\n\r\nFor Web and Deno, no install is required! Just put this line at the top of your file:\r\n\r\n```typescript\r\nimport { inflate } from 'https://cdn.skypack.dev/alea-generator';\r\n```\r\n\r\nIf you want type support with skypack, follow the directions [here]('https://docs.skypack.dev/skypack-cdn/code/javascript#using-skypack-urls-in-typescript')\r\n\r\n#### UNPKG <!-- omit in TOC -->\r\n\r\n```html\r\n<script src=\"https://unpkg.com/alea-generator\"></script>\r\n```\r\n\r\nAnd use it like you would any other package from UNPKG\r\n\r\n## π§ Running the tests\r\n\r\nThe basic set of tests are in the test script and the coverage script. Just run those using your perferred package manager (npm, yarn, pnpm, etc.) if you want to make sure nothing has broken.\r\n\r\n## π Usage\r\n\r\nHere's the great part: thanks to [microbundle](https://github.com/developit/microbundle), this package supports CJS, UMD, and ESM formats.\r\nThat means that wherever and however you use this package β in browser or node, with import or require β you _should_ be set, no configuration required.\r\n\r\n<!-- TODO -->\r\n\r\nThere are two exports from this package: alea and aleaFactory. If you just want to use a higher quality drop in replacement for Math.random, alea is the pick. For the notably expanded functionality, use aleaFactory.\r\n\r\n```typescript\r\nimport { alea as random } from 'alea-generator';\r\n\r\nconsole.log(random()); // 0.6198398587293923\r\nconsole.log(random()); // 0.0231225231354864\r\nconsole.log(random()); // 0.9802844453866831\r\n\r\n// all outputs will be greater than or equal to 0 and less than 1\r\n```\r\n\r\n```typescript\r\nimport { aleaFactory } from 'alea-generator';\r\n\r\n/********************************************/\r\n/* Seeding */\r\n/********************************************/\r\n\r\n// the aleaFactory is optionally seedable\r\nconst randomGeneratorNoSeed = aleaFactory();\r\n\r\n// the seed can be a number\r\nconst randomGeneratorWithNumberSeed = aleaFactory(123);\r\n\r\n// or the seed can be a string\r\nconst randomGeneratorWithTextSeed = aleaFactory('special seed');\r\n\r\n// or any object with a toString method\r\nconst objectSeed = {\r\n\tnum: 45,\r\n\tnum2: 47,\r\n\ttoString: () => `${objectSeed.num} + ${objectSeed.num2}`,\r\n};\r\nconst randomGeneratorWithObjectSeed = aleaFactory(objectSeed);\r\n// be careful with this, if you haven't implemented a toString method\r\n// as it will inherit one from the prototype chain and default to [object Object]\r\n\r\n/********************************************/\r\n/* Methods */\r\n/********************************************/\r\nconst randomGenerator = aleaFactory('1277182878230');\r\n\r\n// random is the drop in replacement for Math.random\r\n// it will generate numbers greater than or equal to 0 and less than 1\r\nconsole.log(randomGenerator.random()); // 0.6198398587293923\r\nconsole.log(randomGenerator.random()); // 0.8385338634252548\r\n\r\n// uint32 will generate integers greater than or equal to 0 and less than 2^32\r\nconsole.log(randomGenerator.uint32()); // 715789690\r\nconsole.log(randomGenerator.uint32()); // 4250\r\n\r\n// Fract53 will generate a 53 bit number between 0 and 1 (like random but higher precision)\r\nconsole.log(randomGenerator.fract53()); // 0.16665777435687268;\r\nconsole.log(randomGenerator.fract53()); // 0.00011322738143160205;\r\n\r\n// exportState and importState allow for exact state duplication\r\nconst stateBeforeRunning = randomGenerator.exportState();\r\nconsole.log(randomGenerator.random()); // 0.7692187615214618\r\nconsole.log(randomGenerator.random()); // 0.2316584344522553\r\n\r\nrandomGenerator.importState(stateBeforeRunning);\r\nconsole.log(randomGenerator.random()); // 0.7692187615214618\r\nconsole.log(randomGenerator.random()); // 0.2316584344522553\r\n\r\nconst otherGenerator = aleaFactory().importState(stateBeforeRunning);\r\nconsole.log(otherGenerator.random()); // 0.7692187615214618\r\nconsole.log(otherGenerator.random()); // 0.2316584344522553\r\n```\r\n\r\n<!-- LICENSE -->\r\n\r\n## π License and Shoutouts\r\n\r\nDistributed under the MIT License. See `LICENSE` for more information. This is a typescript port of an [alea generator for javascript](https://github.com/coverslide/node-alea/) which was itself a packaged version of the implementation by Johannes BaagΓΈe which you can read more about [here](https://web.archive.org/web/20120619002808/http://baagoe.org/en/wiki/Better_random_numbers_for_javascript)\r\n\r\n## βοΈ Authors\r\n\r\nFind me [@illumincrotty](https://github.com/illumincrotty) on github or [@illumincrotty](https://twitter.com/illumincrotty) on twitter\r\n\r\n## π¨ Similar Tools\r\n\r\nIf this tool isn't working for you, try one of these:\r\n\r\n- [ISAAC, A cryptographically secure random number generator](\"https://github.com/macmcmeans/isaacCSPRNG\")\r\n- [The new standard crypto web api for cryptographically secure random numbers](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues)\r\n- [Prando](https://www.npmjs.com/package/prando)\r\n"
}