{"id":21745554,"url":"https://github.com/manuartero/randomjs","last_synced_at":"2025-08-04T16:33:30.019Z","repository":{"id":143759296,"uuid":"358278584","full_name":"manuartero/randomjs","owner":"manuartero","description":"Humanized random functions in js","archived":false,"fork":false,"pushed_at":"2021-04-26T13:42:43.000Z","size":460,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-24T16:14:37.213Z","etag":null,"topics":["normal-distribution","random","random-generation"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/manuartero.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2021-04-15T13:58:44.000Z","updated_at":"2025-04-14T17:52:45.000Z","dependencies_parsed_at":"2023-06-12T09:00:11.532Z","dependency_job_id":null,"html_url":"https://github.com/manuartero/randomjs","commit_stats":null,"previous_names":["manutero/randomjs"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/manuartero/randomjs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/manuartero%2Frandomjs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/manuartero%2Frandomjs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/manuartero%2Frandomjs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/manuartero%2Frandomjs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/manuartero","download_url":"https://codeload.github.com/manuartero/randomjs/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/manuartero%2Frandomjs/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268723073,"owners_count":24296544,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-04T02:00:09.867Z","response_time":79,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["normal-distribution","random","random-generation"],"created_at":"2024-11-26T07:15:58.807Z","updated_at":"2025-08-04T16:33:29.991Z","avatar_url":"https://github.com/manuartero.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![BlueBall](https://github.com/manutero/randomjs/actions/workflows/blue-ball.yaml/badge.svg)](https://github.com/manutero/randomjs/actions/workflows/blue-ball.yaml)\n[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)\n[![Language grade: JavaScript](https://img.shields.io/lgtm/grade/javascript/g/manutero/randomjs.svg?logo=lgtm\u0026logoWidth=18)](https://lgtm.com/projects/g/manutero/randomjs/context:javascript)\n\n# @manutero/randomjs\n\n\u003e Humanized random functions.\n\n## Installation\n\n```bash\nnpm i @manutero/randomjs\n```\n\n...or just copy one file to your project if you want to avoid more deps:\n\n```bash\ncurl -s https://raw.githubusercontent.com/manutero/randomjs/main/index.js \u003e {my-awesome-project}/src/random.js\n```\n\n## Quick Start\n\n```js\nconst { RandomGenerator } = require(\"@manutero/randomjs\");\n\nconst random = RandomGenerator();\n\nrandom.number(1, 10); // natural number between [a,b]\n// =\u003e 3\n\nrandom.normal(-100, 100); // natural number between [a,b] considering a normal distribution\n// =\u003e -4\n\nrandom.roll(\"2d6\"); // roll some dices\n// =\u003e [2, 5]\n\nrandom.bet(42.4); // bet with (0..100%) chance of success.\n// =\u003e false\n\nrandom.unit(); // random unit (0 - 1)\n// =\u003e 0.7580578515771776\n\nrandom.pickOne([1, 2, 3, 4, 5]); // pick one element (all elements have same weight)\n// =\u003e 1\n\nrandom.pickOne([\n  // pick one element (considering different weights)\n  { key: 1, weight: 1 },\n  { key: 2, weight: 1 },\n  { key: 3, weight: 9999 },\n  { key: 4, weight: 1 },\n]);\n// =\u003e {key: 3, weight: 9999}\n```\n\n## Dependencies\n\nNone. Just a plain .js file wrapping `Math.random()`\n\n## Use Case\n\nSome uses cases I can think of...\n\n- you just want a natural random between 1 and 10 while you aren't concern about pure mathematical correctness.\n- you just want to pick an element from an array while not adding another dependency to your project (but copying 1 easy-to-read js file is ok)\n- you just want to roll some dices while would be great that other guy has tested that function already.\n\n\u003e If you're looking for a mathematically correct Random generator OR cryptographically secure random numbers, try with [random-js](https://github.com/ckknight/random-js) instead\n\n## Method Detail\n\n### `.number(a:int, b: int) -\u003e int`\n\nNatural number between [a, b] \\(inclusive).\n\n```js\n\u003e random.number(1,10)\n6\n\u003e random.number(1,10)\n3\n\u003e random.number(1,10)\n9\n\u003e random.number(1,10)\n9\n\u003e random.number(1,10)\n10\n```\n\n### `.normal(a: int, b: int, {skew?: int}) -\u003e int`\n\nnatural number between [a, b] \\(inclusive) considering a normal distribution.\n\n```js\n\u003e random.normal(1,10)\n5\n\u003e random.normal(1,10)\n4\n\u003e random.normal(1,10)\n6\n\u003e random.normal(1,10)\n4\n\u003e random.normal(1,10)\n6\n\u003e random.normal(1,10)\n6\n\u003e random.normal(1,10)\n5\n```\n\n### `.roll(dice: str) -\u003e [number]`\n\nRoll some dices\n\n```js\n\u003e random.roll('1d6')\n[5]\n\u003e random.roll('2d12')\n[4, 10]\n\u003e random.roll('3d6')\n[1, 1, 5]\n\u003e random.roll('D3')\n[2]\n\u003e random.roll('1D12')\n[12]\n```\n\n### `.bet(options: float) -\u003e boolean`\n\nbet with a percentage of success (expected a number between 0 - 100)\n\n```js\n\u003e random.bet(40.0)\nfalse\n\u003e random.bet(40.0)\ntrue\n\u003e random.bet(40.0)\nfalse\n\u003e random.bet(0.0)\nfalse\n\u003e random.bet(100.0)\ntrue\n```\n\n### `.unit() -\u003e float`\n\nunit random between (0, 1) \\(not inclusive)\n\n```js\n\u003e random.unit()\n0.7580578515771776\n\u003e random.unit()\n0.11270887637510896\n\u003e random.unit()\n0.028320789337158203\n```\n\n### `.pickOne(choices: [T], opts?: { weightKey\u003e: string}) -\u003e T`:\n\npick one element from an array\n\n```js\n\u003e random.pickOne([1 ,2, 3, 4, 5])\n3\n\u003e random.pickOne([1 ,2, 3, 4, 5])\n5\n\u003e random.pickOne([1 ,2, 3, 4, 5])\n5\n\u003e random.pickOne([1 ,2, 3, 4, 5])\n1\n```\n\nPick one element from an array with weight\n\n```js\n\u003e random.pickOne([\n    { key: 1, weight: 10 },\n    { key: 2, weight: 10 },\n    { key: 3, weight: 10 },\n    { key: 4, weight: 10 },\n    { key: 5, weight: 10 }\n])\n{ key: 5, weight: 10 }\n\u003e random.pickOne([\n    { key: 1, weight: 10 },\n    { key: 2, weight: 10 },\n    { key: 3, weight: 10 },\n    { key: 4, weight: 99999 },\n    { key: 5, weight: 10 }\n])\n{ key: 4, weight: 99999 }\n\u003e random.pickOne(\n  [\n    { relevance: 10 },\n    { relevance: 10 },\n    { relevance: 10 },\n    { relevance: 99999 },\n    { relevance: 10 }\n  ],\n  { weightKey: 'relevance' })\n{ relevance: 99999 }\n```\n\n### `.seed`\n\nGet the internal `seed`\n\n```js\n\u003e random.seed\n72779816406.94533\n```\n\n### `constructor()`\n\nForce the internal `seed`\n\n```js\n\u003e r1 = RandomGenerator(42)\n\u003e r2 = RandomGenerator(42)\n\u003e r1.unit()\n0.6011037519201636\n\u003e r2.unit()\n0.6011037519201636\n\u003e r1.unit() === r2.unit()\ntrue\n```\n\n## License\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmanuartero%2Frandomjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmanuartero%2Frandomjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmanuartero%2Frandomjs/lists"}