{"id":13658541,"url":"https://github.com/parmentf/random-weighted-choice","last_synced_at":"2025-10-17T12:11:22.727Z","repository":{"id":6069779,"uuid":"7295686","full_name":"parmentf/random-weighted-choice","owner":"parmentf","description":"Node.js module to make a random choice among weighted elements of table.","archived":false,"fork":false,"pushed_at":"2022-12-06T06:32:59.000Z","size":145,"stargazers_count":37,"open_issues_count":5,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-02T19:04:54.633Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/parmentf.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-12-23T14:35:03.000Z","updated_at":"2025-04-02T05:50:15.000Z","dependencies_parsed_at":"2023-01-11T17:02:15.052Z","dependency_job_id":null,"html_url":"https://github.com/parmentf/random-weighted-choice","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parmentf%2Frandom-weighted-choice","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parmentf%2Frandom-weighted-choice/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parmentf%2Frandom-weighted-choice/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parmentf%2Frandom-weighted-choice/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/parmentf","download_url":"https://codeload.github.com/parmentf/random-weighted-choice/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248523095,"owners_count":21118436,"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":[],"created_at":"2024-08-02T05:01:00.542Z","updated_at":"2025-10-17T12:11:22.653Z","avatar_url":"https://github.com/parmentf.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# Random Weighted Choice\n\n[![Build Status](https://secure.travis-ci.org/parmentf/random-weighted-choice.png?branch=master)](http://travis-ci.org/parmentf/random-weighted-choice)\n[![Code Coverage](https://img.shields.io/codecov/c/github/parmentf/random-weighted-choice.svg)](https://codecov.io/github/parmentf/random-weighted-choice)\n\nNode.js module to make a random choice among weighted elements of table.\n\n## Installation\n\nWith [npm](http://npmjs.org) do:\n\n```bash\nnpm install random-weighted-choice\n```\n\n## Examples\n\nAlthough you can add several times the same id\n\n```js\nvar rwc = require(\"random-weighted-choice\");\nvar table = [\n  { weight: 1, id: \"item1\" }, // Element 1\n  { weight: 1, id: \"item2\" }, // Element 2\n  { weight: 4, id: \"item3\" }, // Element with a 4 times likelihood\n  { weight: 2, id: \"item1\" }, // Element 1, weight added with 2 =\u003e 3\n];\nvar choosenItem = rwc(table);\nvar choosenUnlikely = rwc(table, 100); // The last shall be first\nvar choosenDeterministically = rwc(table, 0);\n```\n\nIt is better to not use the same twice, if you want a temperature other than\nthe default one (50).\n\n```js\nvar rwc = require(\"random-weighted-choice\");\nvar table = [\n  { weight: 1, id: \"item1\" }, // Element 1\n  { weight: 1, id: \"item2\" }, // Element 2\n  { weight: 4, id: \"item3\" }, // Element with a 4 times likelihood\n  { weight: 2, id: \"item4\" }, // Element 4\n  { weight: 2, id: \"item5\" },\n];\nvar choosenItem = rwc(table);\nvar choosenUnlikely = rwc(table, 100); // The last shall be first\nvar choosenDeterministically = rwc(table, 0);\n```\n\nWithout temperature (second parameter) or a 50 value, likelihoods are:\n\n```txt\n{ item1: 10%, item2: 10%, item3: 40%, item4: 20%, item5: 20% }\n```\n\nWith a temperature value of 100:\n\n```txt\n{ item1: 30%, item2: 30%, item3: 0%, item4: 20%, item5: 20% }\n```\n\nWith a temperature value of 0, modified weights are:\n\n```txt\n{ item1: 0, item2: 0, item3: 8, item4: 2, item5: 2 }\n```\n\n## Usage\n\n### random-weighted-choice(Array table, Number temperature = 50)\n\nReturn the `id` of the chosen item from `table`.\n\nThe `table` parameter should contain an Array. Each item of that Array must\nbean object, with at least `weight` and `id` property.\n\nWeight values are relative to each other. They are integers.\n\nWhen the sum of the weight values is `null`, `null` is returned (can't choose).\n\nWhen the Array is empty, `null` is returned.\n\nMore explanations on how it works on [Everything2](http://everything2.com/title/Blackboard+temperature).\n\n## Also\n\n- \u003chttps://github.com/Schoonology/weighted\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparmentf%2Frandom-weighted-choice","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fparmentf%2Frandom-weighted-choice","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparmentf%2Frandom-weighted-choice/lists"}