{"id":13809177,"url":"https://github.com/JohnJocoo/weighted_random","last_synced_at":"2025-05-14T05:33:25.248Z","repository":{"id":241622679,"uuid":"806263947","full_name":"JohnJocoo/weighted_random","owner":"JohnJocoo","description":"Elixir weighted random pick library optimised for quick take_one and take_n operations","archived":false,"fork":false,"pushed_at":"2024-05-29T10:47:05.000Z","size":21,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-11-05T19:47:06.431Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://hexdocs.pm/better_weighted_random","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JohnJocoo.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":"2024-05-26T20:21:33.000Z","updated_at":"2024-09-23T11:06:13.000Z","dependencies_parsed_at":"2024-08-04T01:10:05.574Z","dependency_job_id":"2d9bf532-d88d-4bfe-a2de-50eef405ed83","html_url":"https://github.com/JohnJocoo/weighted_random","commit_stats":null,"previous_names":["johnjocoo/weighted_random"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohnJocoo%2Fweighted_random","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohnJocoo%2Fweighted_random/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohnJocoo%2Fweighted_random/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohnJocoo%2Fweighted_random/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JohnJocoo","download_url":"https://codeload.github.com/JohnJocoo/weighted_random/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225277987,"owners_count":17448798,"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-04T01:02:04.642Z","updated_at":"2024-11-19T01:30:50.818Z","avatar_url":"https://github.com/JohnJocoo.png","language":"Elixir","funding_links":[],"categories":["Miscellaneous"],"sub_categories":[],"readme":"# WeightedRandom\n\nElixir weighted random pick library optimised for quick `take_one` and `take_n` operations. \n\n`take_one\\1` and `take_n\\2` behaviours are compatible with `Enum.random\\1` and `Enum.take_random\\2` and can almost be drop-in replacements, except that `WeightedRandom` accepts elements in format `{element :: any(), weight :: number()}`.\n\n## Installation\n\nThe package can be installed\nby adding `better_weighted_random` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:better_weighted_random, \"~\u003e 0.1.0\"}\n  ]\nend\n```\n\n## Usage\n\n`WeightedRandom.take_one\\1` returns one random element from input, with probability based on weights.\n\n```elixir\niex\u003e :rand.seed(:exsss, {100, 101, 102})\niex\u003e WeightedRandom.take_one([{:'1', 0.5}, {:'2', 1.0}, {:'3', 2.0}])\n:\"3\"\n```\n\n`WeightedRandom.take_n\\1` returns n random elements from input, with probability based on weights. Similar to `Enum.take_random\\2` element can not be picked twice.\n\n```elixir\niex\u003e :rand.seed(:exsss, {100, 101, 102})\niex\u003e WeightedRandom.take_n([{:'1', 0.5}, {:'2', 1.0}, {:'3', 2.0}], 2)\n[:\"3\", :\"1\"]\n```\n\n## Probability and weight\nProbability of picking an element directly depends on its weight. `Probability = element_weight / sum(all_weights)`\n\n```elixir\niex\u003e :rand.seed(:exsss, {100, 101, 102})\niex\u003e Enum.map(1..1000, fn _ -\u003e WeightedRandom.take_one([{:'1', 1.0}, {:'2', 2.0}, {:'3', 1.0}]) end) |\u003e Enum.frequencies()\n%{\n  \"1\": 231, \n  \"2\": 522, \n  \"3\": 247\n} \n```\n\n## Searcher and quick lookups\n\nYou can create searcher with `WeightedRandom.create_searcher\\1` for optimised lookups with `take_one\\1` and `take_n\\2`.\n\n```elixir\niex\u003e searcher = WeightedRandom.create_searcher([{:'1', 1.0}, {:'2', 2.0}, {:'3', 1.0}])\n\niex\u003e :rand.seed(:exsss, {100, 101, 102})\niex\u003e WeightedRandom.take_one(searcher)\n:\"3\"\n\niex\u003e rand.seed(:exsss, {100, 101, 102})\niex\u003e WeightedRandom.take_n(searcher, 2)\n[:\"3\", :\"1\"]\n```\n\nUsing searcher is quicker if you do repetative calls.\n\n```elixir\nelements = Enum.map(1..10000, fn i -\u003e {:\"el#{i}\", :rand.uniform()} end)\nsearcher = WeightedRandom.create_searcher(elements)\n```\n\n### WeightedRandom.take_one/1\n\n```\nName                             ips        average  deviation         median         99th %\ntake_one with searcher      787.69 K     0.00127 ms  ±2423.14%     0.00109 ms     0.00180 ms\ntake_one with list           0.137 K        7.29 ms    ±17.57%        7.03 ms       11.42 ms\n\nComparison:\ntake_one with searcher      787.69 K\ntake_one with list           0.137 K - 5740.48x slower +7.29 ms\n```\n\n### WeightedRandom.take_n/2 take 100 elements\n\n```\nName                               ips        average  deviation         median         99th %\ntake_n 100 with searcher        7.26 K       0.138 ms    ±30.77%       0.124 ms        0.32 ms\ntake_n 100 with list           0.106 K        9.43 ms    ±12.97%        8.96 ms       12.29 ms\n\nComparison:\ntake_n 100 with searcher        7.26 K\ntake_n 100 with list           0.106 K - 68.46x slower +9.30 ms\n```\n\n## Docs\n\nDocs are availiable at \u003chttps://hexdocs.pm/better_weighted_random\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FJohnJocoo%2Fweighted_random","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FJohnJocoo%2Fweighted_random","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FJohnJocoo%2Fweighted_random/lists"}