{"id":30807624,"url":"https://github.com/ichi-h/weighted_rand","last_synced_at":"2025-09-06T02:48:14.051Z","repository":{"id":46181881,"uuid":"425530667","full_name":"ichi-h/weighted_rand","owner":"ichi-h","description":"A weighted random sampling crate using Walker's Alias Method.","archived":false,"fork":false,"pushed_at":"2023-10-08T20:03:52.000Z","size":90,"stargazers_count":9,"open_issues_count":5,"forks_count":4,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-24T07:38:24.932Z","etag":null,"topics":["alias-method","probability","rust","weighted-probability"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/weighted_rand","language":"Rust","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/ichi-h.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2021-11-07T14:48:08.000Z","updated_at":"2025-08-14T12:29:39.000Z","dependencies_parsed_at":"2022-08-31T01:24:23.118Z","dependency_job_id":null,"html_url":"https://github.com/ichi-h/weighted_rand","commit_stats":{"total_commits":55,"total_committers":2,"mean_commits":27.5,"dds":"0.018181818181818188","last_synced_commit":"cbbd2453745b163b47f4251b6a7acae9987f729d"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ichi-h/weighted_rand","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ichi-h%2Fweighted_rand","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ichi-h%2Fweighted_rand/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ichi-h%2Fweighted_rand/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ichi-h%2Fweighted_rand/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ichi-h","download_url":"https://codeload.github.com/ichi-h/weighted_rand/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ichi-h%2Fweighted_rand/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273850843,"owners_count":25179357,"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","status":"online","status_checked_at":"2025-09-06T02:00:13.247Z","response_time":2576,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-method","probability","rust","weighted-probability"],"created_at":"2025-09-06T02:48:10.424Z","updated_at":"2025-09-06T02:48:14.043Z","avatar_url":"https://github.com/ichi-h.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# weighted_rand\n\n[![weighted_rand](https://github.com/ichi-h/weighted_rand/actions/workflows/weighted_rand.yml/badge.svg)](https://github.com/ichi-h/weighted_rand/actions/workflows/weighted_rand.yml)\n[![Crates.io](https://img.shields.io/crates/v/weighted_rand)](https://crates.io/crates/weighted_rand)\n[![docs.rs](https://img.shields.io/docsrs/weighted_rand)](https://docs.rs/weighted_rand)\n[![Crates.io](https://img.shields.io/crates/l/weighted_rand)](LICENSE-APACHE)\n\nA weighted random sampling crate using Walker's Alias Method.\n\nWalker's Alias Method (WAM) is one method for performing weighted random sampling.  \nWAM weights each index of a array by giving two pieces of information: an alias to a different index and a probability to decide whether to jump to that index.\n\nWAM is a very fast algorithm, and its computational complexity of the search is O(1).  \nThe difference in complexity between WAM and the Cumulative Sum Method is as follows.\n\n|       Algorithm       | Building table |  Search  |\n| :-------------------: | :------------: | :------: |\n| Walker's Alias Method |      O(N)      |   O(1)   |\n| Cumulative Sum Method |      O(N)      | O(log N) |\n\nThe API documentation is [here](https://docs.rs/weighted_rand).\n\n## Usage\n\nAdd this to your Cargo.toml:\n\n```toml\n[dependencies]\nweighted_rand = \"0.4\"\n```\n\n## Example\n\n```rust\nuse weighted_rand::builder::WalkerTableBuilder;\n\nfn main() {\n    let fruit = [\"Apple\", \"Banana\", \"Orange\", \"Peach\"];\n\n    // Define the weights for each index corresponding\n    // to the above list.\n    // In the following case, the ratio of each weight\n    // is \"2 : 1 : 7 : 0\", and the output probabilities\n    // for each index are 0.2, 0.1, 0.7 and 0.\n    let index_weights = [2, 1, 7, 0];\n\n    let builder = WalkerTableBuilder::new(\u0026index_weights);\n    let wa_table = builder.build();\n\n    for i in (0..10).map(|_| wa_table.next()) {\n        println!(\"{}\", fruit[i]);\n    }\n}\n```\n\nAlso, `index_weiaghts` supports `\u0026[f32]`, like:\n\n```rust\nuse rand;\nuse weighted_rand::builder::*;\n\nfn main() {\n    // Coin with a 5% higher probability of heads than tails\n    let cheating_coin = [\"Heads!\", \"Tails!\"];\n    let index_weights = [0.55, 0.45];\n\n    let builder = WalkerTableBuilder::new(\u0026index_weights);\n    let wa_table = builder.build();\n\n    // If you want to process something in a large number of\n    // loops, we recommend using the next_rng method with an\n    // external ThreadRng instance.\n    let mut result = [\"\"; 10000];\n    let mut rng = rand::thread_rng();\n    for r in \u0026mut result {\n        let j = wa_table.next_rng(\u0026mut rng);\n        *r = cheating_coin[j];\n    }\n\n    // println!(\"{:?}\", result);\n}\n```\n\n## License\n\nLicensed under either of\n\n- Apache License, Version 2.0\n  ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)\n- MIT license\n  ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)\n\nat your option.\n\n## Contribution\n\nUnless you explicitly state otherwise, any contribution intentionally submitted\nfor inclusion in the work by you, as defined in the Apache-2.0 license, shall be\ndual licensed as above, without any additional terms or conditions.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fichi-h%2Fweighted_rand","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fichi-h%2Fweighted_rand","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fichi-h%2Fweighted_rand/lists"}