{"id":15117164,"url":"https://github.com/alvarocastro/pick-random-weighted","last_synced_at":"2025-08-03T23:39:54.605Z","repository":{"id":35022900,"uuid":"197639076","full_name":"alvarocastro/pick-random-weighted","owner":"alvarocastro","description":"Ultra fast and lightweight function to pick a random item from a weighted list.","archived":false,"fork":false,"pushed_at":"2023-04-05T21:57:37.000Z","size":1092,"stargazers_count":7,"open_issues_count":9,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-17T21:54:07.094Z","etag":null,"topics":["array","arrays","choose","decision-making","javascript","random","weighted","weighted-random"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/pick-random-weighted","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/alvarocastro.png","metadata":{"files":{"readme":"README.md","changelog":null,"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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-07-18T18:39:13.000Z","updated_at":"2024-12-12T17:05:47.000Z","dependencies_parsed_at":"2024-06-19T19:15:26.165Z","dependency_job_id":null,"html_url":"https://github.com/alvarocastro/pick-random-weighted","commit_stats":{"total_commits":100,"total_committers":3,"mean_commits":"33.333333333333336","dds":0.4,"last_synced_commit":"6ee208b1edb3586cddc1f860f54e906bca9b9f7b"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/alvarocastro/pick-random-weighted","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alvarocastro%2Fpick-random-weighted","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alvarocastro%2Fpick-random-weighted/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alvarocastro%2Fpick-random-weighted/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alvarocastro%2Fpick-random-weighted/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alvarocastro","download_url":"https://codeload.github.com/alvarocastro/pick-random-weighted/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alvarocastro%2Fpick-random-weighted/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265686691,"owners_count":23811211,"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","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":["array","arrays","choose","decision-making","javascript","random","weighted","weighted-random"],"created_at":"2024-09-26T01:45:51.290Z","updated_at":"2025-08-03T23:39:54.404Z","avatar_url":"https://github.com/alvarocastro.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# pick-random-weighted\n\n[![NPM](https://img.shields.io/npm/v/pick-random-weighted)](https://www.npmjs.com/package/pick-random-weighted)\n[![Build status](https://img.shields.io/github/workflow/status/alvarocastro/pick-random-weighted/build)](https://github.com/alvarocastro/pick-random-weighted/actions?query=workflow%3Abuild)\n[![Maintainability status](https://img.shields.io/codeclimate/maintainability/alvarocastro/pick-random-weighted)](https://codeclimate.com/github/alvarocastro/pick-random-weighted/maintainability)\n[![Coverage status](https://img.shields.io/coveralls/github/alvarocastro/pick-random-weighted)](https://coveralls.io/github/alvarocastro/pick-random-weighted?branch=master)\n[![Bundle size](https://img.shields.io/bundlephobia/min/pick-random-weighted)](https://bundlephobia.com/result?p=pick-random-weighted)\n[![Code style: XO](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/xojs/xo)\n[![Release: Semantic](https://img.shields.io/badge/%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)\n\nSimple, fast and lightweight function to pick a random element from a weighted array.\n\n- [Install](#install)\n- [Usage](#usage)\n- [Randomness](#random-number-generation)\n- [Contributing](#contributing)\n- [Support](#support)\n\n## Install\n\n```bash\nnpm install pick-random-weighted\n```\n\n## Usage\n\nThe package contains a `pick` function that just works out-of-the box.\n\n```js\nimport pick from 'pick-random-weighted';\n\nconst colors = [\n\t['Red', 30],\n\t['Green', 20],\n\t['Blue', 40]\n];\n\nconst color = pick(colors);\n```\n\nIt also contains a `Picker` class that can be instantiated to create a custom picker.\n\n```js\nimport { Picker } from 'pick-random-weighted';\n\nconst picker = new Picker();\nconst colors = [\n\t['Red', 30],\n\t['Green', 20],\n\t['Blue', 40]\n];\n\nconst color = Picker.pick(colors);\n```\n\n### pick(values)\n\nReturns a random value from the `values` array.\n\n##### values\n\nType: `Array`\n\nList of values to pick from.\n\nEach element should be provided in the format `[value, weight]`.\n\n## Random number generation\n\nBy default, and to keep a small footprint, the library uses `Math.random()` to generate the random number to pick the value.\n\nIf you need you can define a custom function to generate random values, you can create a function that returns fixed values to use on your unit tests or implement a more specialized library like [random-js](https://www.npmjs.com/package/random-js).\n\n### Using a custom random generator\n\nBy overwriting `pick.random` you can define your custom function.\n\u003e Remember the returned value should be a number within the [0,1) range.\n\n```js\nimport { Picker } from 'pick-random-weighted';\n\nconst picker = new Picker(function () {\n\treturn 0.3;\n});\n\nconst colors = [\n\t['Red', 30],\n\t['Green', 20],\n\t['Blue', 40]\n];\n\nconst color = Picker.pick(colors);\n// Will always return 'Green' since our custom random generator function always returns the same value.\n```\n\n## Contributing\n\nContributions are always welcome! Please run `npm test` beforehand to ensure everything is ok.\n\n## Support\n\nIf you use this package please consider starring it :)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falvarocastro%2Fpick-random-weighted","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falvarocastro%2Fpick-random-weighted","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falvarocastro%2Fpick-random-weighted/lists"}