{"id":21585962,"url":"https://github.com/magiclen/random-pick","last_synced_at":"2025-10-28T05:36:03.115Z","repository":{"id":57658510,"uuid":"146406716","full_name":"magiclen/random-pick","owner":"magiclen","description":"Pick an element from a slice randomly by given weights.","archived":false,"fork":false,"pushed_at":"2023-09-10T03:22:02.000Z","size":31,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-28T07:51:33.466Z","etag":null,"topics":["random","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","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/magiclen.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":"2018-08-28T07:09:15.000Z","updated_at":"2023-03-04T02:48:06.000Z","dependencies_parsed_at":"2025-01-24T15:40:58.861Z","dependency_job_id":null,"html_url":"https://github.com/magiclen/random-pick","commit_stats":null,"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Frandom-pick","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Frandom-pick/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Frandom-pick/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Frandom-pick/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/magiclen","download_url":"https://codeload.github.com/magiclen/random-pick/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244189826,"owners_count":20412991,"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":["random","rust"],"created_at":"2024-11-24T15:12:15.974Z","updated_at":"2025-10-28T05:35:58.055Z","avatar_url":"https://github.com/magiclen.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"Random Pick\n====================\n\n[![CI](https://github.com/magiclen/random-pick/actions/workflows/ci.yml/badge.svg)](https://github.com/magiclen/random-pick/actions/workflows/ci.yml)\n\nPick an element from a slice randomly by given weights.\n\n## Example\n\n```rust\nenum Prize {\n    Legendary,\n    Rare,\n    Enchanted,\n    Common,\n}\n\nlet prize_list = [Prize::Legendary, Prize::Rare, Prize::Enchanted, Prize::Common]; // available prizes\n\nlet slice = \u0026prize_list;\nlet weights = [1, 5, 15, 30]; // a scale of chance of picking each kind of prize\n\nlet n = 1000000;\nlet mut counter = [0usize; 4];\n\nfor _ in 0..n {\n    let picked_item = random_pick::pick_from_slice(slice, \u0026weights).unwrap();\n\n    match picked_item {\n        Prize::Legendary=\u003e{\n            counter[0] += 1;\n           }\n        Prize::Rare=\u003e{\n            counter[1] += 1;\n        }\n        Prize::Enchanted=\u003e{\n            counter[2] += 1;\n        }\n        Prize::Common=\u003e{\n            counter[3] += 1;\n        }\n    }\n}\n\nprintln!(\"{}\", counter[0]); // Should be close to 20000\nprintln!(\"{}\", counter[1]); // Should be close to 100000\nprintln!(\"{}\", counter[2]); // Should be close to 300000\nprintln!(\"{}\", counter[3]); // Should be close to 600000\n```\n\nThe length of the slice is usually an integral multiple (larger than zero) of that of weights.\n\nIf you have multiple slices, you don't need to use extra space to concat them, just use the `pick_from_multiple_slices` function, instead of `pick_from_slice`.\n\nBesides picking a single element from a slice or slices, you can also use `pick_multiple_from_slice` and `pick_multiple_from_multiple_slices` functions. Their overhead is lower than that of non-multiple-pick functions with extra loops.\n\n## Crates.io\n\nhttps://crates.io/crates/random-pick\n\n## Documentation\n\nhttps://docs.rs/random-pick\n\n## License\n\n[MIT](LICENSE)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagiclen%2Frandom-pick","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmagiclen%2Frandom-pick","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagiclen%2Frandom-pick/lists"}