{"id":15168618,"url":"https://github.com/titsuki/raku-random-choice","last_synced_at":"2026-01-22T20:08:59.651Z","repository":{"id":43517136,"uuid":"173594043","full_name":"titsuki/raku-Random-Choice","owner":"titsuki","description":"A Raku alias method implementation","archived":false,"fork":false,"pushed_at":"2022-10-26T14:53:31.000Z","size":31,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-15T09:35:23.788Z","etag":null,"topics":["alias","choice","method","perl6","raku","rakulang","random","zef"],"latest_commit_sha":null,"homepage":"","language":"Raku","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"artistic-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/titsuki.png","metadata":{"files":{"readme":"README.md","changelog":"Changes","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-03-03T15:38:46.000Z","updated_at":"2022-12-17T08:14:19.000Z","dependencies_parsed_at":"2023-01-20T05:15:14.119Z","dependency_job_id":null,"html_url":"https://github.com/titsuki/raku-Random-Choice","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/titsuki%2Fraku-Random-Choice","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/titsuki%2Fraku-Random-Choice/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/titsuki%2Fraku-Random-Choice/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/titsuki%2Fraku-Random-Choice/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/titsuki","download_url":"https://codeload.github.com/titsuki/raku-Random-Choice/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248060640,"owners_count":21041199,"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":["alias","choice","method","perl6","raku","rakulang","random","zef"],"created_at":"2024-09-27T06:23:51.757Z","updated_at":"2026-01-22T20:08:59.619Z","avatar_url":"https://github.com/titsuki.png","language":"Raku","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/titsuki/raku-Random-Choice.svg?branch=master)](https://travis-ci.org/titsuki/raku-Random-Choice)\n\nNAME\n====\n\nRandom::Choice - A Raku alias method implementation\n\nSYNOPSIS\n========\n\n```perl6\nuse Random::Choice;\n\nsay choice(:size(8), :p([0.1, 0.1, 0.1, 0.7])); # (3 1 0 3 3 3 3 3)\nsay choice(:p([0.1, 0.1, 0.1, 0.7])); # 3\n```\n\nDESCRIPTION\n===========\n\nRandom::Choice is a Raku alias method implementation. Alias method is an efficient algorithm for sampling from a discrete probability distribution.\n\nMETHODS\n-------\n\n### choice\n\nDefined as:\n\n    multi sub choice(:@p! --\u003e Int) is export\n    multi sub choice(Int :$size!, :@p! --\u003e List)\n\nReturns a sample which is an Int value or a List. Where `:@p` is the probabilities associated with each index and `:$size` is the sample size.\n\nFAQ\n===\n\nIs `Random::Choice` faster than Mix.roll?\n-----------------------------------------\n\nThe answer is YES when you roll a large biased dice or try to roll a dice many times; but NO when a biased dice is small or try to roll a dice few times.\n\nWhy? There are some possible reasons:\n\n  * `Random::Choice` employs O(N) + O(1) algorithm whereas `Mix.roll` employs O(N) + O(N) algorithm (rakudo 2018.12).\n\n  * `Mix.roll` is directly written in nqp. In general, nqp-powered code is faster than naive-Raku-powered code when they take small input.\n\n  * Both algorithms take O(N) initialization cost; however, the actual cost of `Mix.roll` is slightly less than `Random::Choice`.\n\nA benchmark result is here (For more info, see `example/bench.p6`):\n\n### A Benchmark Result\n\n\u003cimg src=\"./example/bench.svg\" alt=\"benchmark result\"\u003e\n\n### The Comparison Table on the Benchmark\n\n```bash\n$ perl6 example/bench.p6 \nBenchmark: \nTiming 1000 iterations of Mix(size=10, @p.elems=10) , Random::Choice(size=10, @p.elems=10)...\nMix(size=10, @p.elems=10) : 0.076 wallclock secs (0.086 usr 0.003 sys 0.089 cpu) @ 13154.606/s (n=1000)\nRandom::Choice(size=10, @p.elems=10): 0.122 wallclock secs (0.137 usr 0.008 sys 0.145 cpu) @ 8210.383/s (n=1000)\nO--------------------------------------O---------O----------------------------O--------------------------------------O\n|                                      | Rate    | Mix(size=10, @p.elems=10)  | Random::Choice(size=10, @p.elems=10) |\nO======================================O=========O============================O======================================O\n| Mix(size=10, @p.elems=10)            | 13155/s | --                         | -42%                                 |\n| Random::Choice(size=10, @p.elems=10) | 8210/s  | 73%                        | --                                   |\nO--------------------------------------O---------O----------------------------O--------------------------------------O\nBenchmark: \nTiming 1000 iterations of Mix(size=1000, @p.elems=10) , Random::Choice(size=1000, @p.elems=10)...\nMix(size=1000, @p.elems=10) : 1.879 wallclock secs (1.892 usr 0.000 sys 1.892 cpu) @ 532.130/s (n=1000)\nRandom::Choice(size=1000, @p.elems=10): 0.097 wallclock secs (0.099 usr 0.002 sys 0.101 cpu) @ 10361.621/s (n=1000)\nO----------------------------------------O---------O------------------------------O----------------------------------------O\n|                                        | Rate    | Mix(size=1000, @p.elems=10)  | Random::Choice(size=1000, @p.elems=10) |\nO========================================O=========O==============================O========================================O\n| Mix(size=1000, @p.elems=10)            | 532/s   | --                           | 2141%                                  |\n| Random::Choice(size=1000, @p.elems=10) | 10362/s | -96%                         | --                                     |\nO----------------------------------------O---------O------------------------------O----------------------------------------O\nBenchmark: \nTiming 1000 iterations of Mix(size=10, @p.elems=1000) , Random::Choice(size=10, @p.elems=1000)...\nMix(size=10, @p.elems=1000) : 2.576 wallclock secs (2.560 usr 0.020 sys 2.580 cpu) @ 388.182/s (n=1000)\nRandom::Choice(size=10, @p.elems=1000): 6.010 wallclock secs (6.015 usr 0.032 sys 6.047 cpu) @ 166.398/s (n=1000)\nO----------------------------------------O-------O------------------------------O----------------------------------------O\n|                                        | Rate  | Mix(size=10, @p.elems=1000)  | Random::Choice(size=10, @p.elems=1000) |\nO========================================O=======O==============================O========================================O\n| Mix(size=10, @p.elems=1000)            | 388/s | --                           | -57%                                   |\n| Random::Choice(size=10, @p.elems=1000) | 166/s | 134%                         | --                                     |\nO----------------------------------------O-------O------------------------------O----------------------------------------O\nBenchmark: \nTiming 1000 iterations of Mix(size=100, @p.elems=100), Random::Choice(size=100, @p.elems=100)...\nMix(size=100, @p.elems=100): 1.505 wallclock secs (1.511 usr 0.000 sys 1.511 cpu) @ 664.420/s (n=1000)\nRandom::Choice(size=100, @p.elems=100): 0.619 wallclock secs (0.624 usr 0.000 sys 0.624 cpu) @ 1616.535/s (n=1000)\nO----------------------------------------O--------O-----------------------------O----------------------------------------O\n|                                        | Rate   | Mix(size=100, @p.elems=100) | Random::Choice(size=100, @p.elems=100) |\nO========================================O========O=============================O========================================O\n| Mix(size=100, @p.elems=100)            | 664/s  | --                          | 146%                                   |\n| Random::Choice(size=100, @p.elems=100) | 1617/s | -59%                        | --                                     |\nO----------------------------------------O--------O-----------------------------O----------------------------------------O\nBenchmark: \nTiming 1000 iterations of Mix(size=1000, @p.elems=1000), Random::Choice(size=1000, @p.elems=1000)...\nMix(size=1000, @p.elems=1000): 135.720 wallclock secs (135.946 usr 0.288 sys 136.234 cpu) @ 7.368/s (n=1000)\nRandom::Choice(size=1000, @p.elems=1000): 6.022 wallclock secs (6.031 usr 0.028 sys 6.058 cpu) @ 166.058/s (n=1000)\nO------------------------------------------O--------O-------------------------------O------------------------------------------O\n|                                          | Rate   | Mix(size=1000, @p.elems=1000) | Random::Choice(size=1000, @p.elems=1000) |\nO==========================================O========O===============================O==========================================O\n| Mix(size=1000, @p.elems=1000)            | 7.37/s | --                            | 2158%                                    |\n| Random::Choice(size=1000, @p.elems=1000) | 166/s  | -96%                          | --                                       |\nO------------------------------------------O--------O-------------------------------O------------------------------------------O\n```\n\n### The Environment on the Benchmark\n\n  * `CPU` Ryzen7 5800X (8core)\n\n  * `OS` Debian11 bullseye\n\nAUTHOR\n======\n\ntitsuki \u003ctitsuki@cpan.org\u003e\n\nCOPYRIGHT AND LICENSE\n=====================\n\nCopyright 2019 titsuki\n\nThis library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.\n\nThe algorithm is from:\n\n  * Vose, Michael D. \"A linear algorithm for generating random numbers with a given distribution.\" IEEE Transactions on software engineering 17.9 (1991): 972-975.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftitsuki%2Fraku-random-choice","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftitsuki%2Fraku-random-choice","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftitsuki%2Fraku-random-choice/lists"}