{"id":15713733,"url":"https://github.com/josacar/walker_method","last_synced_at":"2025-04-19T15:47:48.890Z","repository":{"id":56590121,"uuid":"267611357","full_name":"josacar/walker_method","owner":"josacar","description":"A Crystal implementation of Walker's Alias Method for quickly sampling from an array with a given probability distribution","archived":false,"fork":false,"pushed_at":"2021-03-24T18:36:37.000Z","size":8,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-29T09:34:12.783Z","etag":null,"topics":["algorithm","crystal","crystal-language","hacktoberfest","probability","shards","walker-alias"],"latest_commit_sha":null,"homepage":"","language":"Crystal","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/josacar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-05-28T14:25:43.000Z","updated_at":"2023-10-15T23:12:28.000Z","dependencies_parsed_at":"2022-08-15T21:31:12.917Z","dependency_job_id":null,"html_url":"https://github.com/josacar/walker_method","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josacar%2Fwalker_method","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josacar%2Fwalker_method/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josacar%2Fwalker_method/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josacar%2Fwalker_method/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/josacar","download_url":"https://codeload.github.com/josacar/walker_method/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249730571,"owners_count":21317327,"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":["algorithm","crystal","crystal-language","hacktoberfest","probability","shards","walker-alias"],"created_at":"2024-10-03T21:33:07.806Z","updated_at":"2025-04-19T15:47:48.872Z","avatar_url":"https://github.com/josacar.png","language":"Crystal","readme":"# WalkerMethod\n\n[![Build Status](https://travis-ci.org/josacar/walker_method.svg?branch=master)](https://travis-ci.org/josacar/walker_method)\n\nWalker's Alias Method is an O(1) algorithm for selecting elements from an array given a weighted distribution.\n\nFor example, let's say you want to return `:win` 60% of the time, `:lose` 20% of the time, and `:tie` 20% of the time.  You could fill an array with\n6 examples of `:win` and 2 examples each of `:lose` and `:tie`, then pick a random element.  You could also use ranges, picking a random number\nbetween 0.0 and 1.0 and returning `:win` when the number is below 0.6, `:lose` if the number is below 0.8, and `:tie` otherwise.  But, these algorithms are still O(n).\nYou can do better by using a heap or binary search tree, but Walker's Alias Method is better still, with a constant runtime once an O(n)\npre-computation phase has completed.  For this example, you could do:\n\n    selector = WalkerMethod.new([:win, :lose, :tie], [60, 20, 20])\n    selector.random\n\nThis implementation is a port of http://code.activestate.com/recipes/576564-walkers-alias-method-for-random-objects-with-diffe/ from Python to Crystal.  There is also a [Node.js implementation](https://github.com/ThoughtLeadr/Walker-Random-Node).\n\n## Speed\n\nIt's FAST and simple.  It samples from a 50,000 word frequency dictionary 10,000 times in 1.2 milliseconds.\n\n## Installation\n\n\nAdd this to your application's `shard.yml`:\n\n```yaml\ndependencies:\n  walker_method:\n    github: josacar/walker_method\n```\n\n## Usage\n\n```crystal\n# Sampling from an English word frequency dictionary care of http://invokeit.wordpress.com/frequency-word-lists\n\nwords = [\"you\", \"the\", \"i\", \"to\", \"a\", \"and\", \"it\", \"of\", \"that\", \"in\", \"is\", \"me\", \"what\", \"this\", \"for\", \"my\", \"on\", \"your\", \"we\", \"have\", \"do\", \"no\", \"don't\", \"are\", \"be\"]\nfreqs = [4621939, 3957465, 3476773, 2873389, 2551033, 1775393, 1693042, 1531878, 1323823, 1295198, 1242191, 1208959, 1071825, 961194, 898671, 877684, 867296, 834953, 819499, 812625, 799991, 788200, 764177, 743194, 743014]\n\nselector = WalkerMethod.new(words, weights)\nselector.random\n# =\u003e \"and\"\n```\n\n## Thanks to\n\nThanks to original gem contributors of [https://github.com/cantino/walker_method](https://github.com/cantino/walker_method)\n\n## Contributing\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosacar%2Fwalker_method","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjosacar%2Fwalker_method","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosacar%2Fwalker_method/lists"}