{"id":19850519,"url":"https://github.com/mroth/weightedrand","last_synced_at":"2025-05-14T09:06:28.873Z","repository":{"id":39675007,"uuid":"139632670","full_name":"mroth/weightedrand","owner":"mroth","description":":balance_scale: Fast weighted random selection for Go","archived":false,"fork":false,"pushed_at":"2024-11-18T14:18:52.000Z","size":126,"stargazers_count":400,"open_issues_count":5,"forks_count":49,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-12T23:29:42.843Z","etag":null,"topics":["go","golang","random","sampling","weighted-random"],"latest_commit_sha":null,"homepage":"","language":"Go","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/mroth.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-07-03T20:11:35.000Z","updated_at":"2025-03-21T07:10:22.000Z","dependencies_parsed_at":"2024-01-24T16:25:56.226Z","dependency_job_id":"b61c5c4c-005b-4a15-ae45-5676bdd48cf4","html_url":"https://github.com/mroth/weightedrand","commit_stats":{"total_commits":84,"total_committers":3,"mean_commits":28.0,"dds":"0.19047619047619047","last_synced_commit":"82b9ec74aca51314533f5ad81822c7b722021e58"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mroth%2Fweightedrand","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mroth%2Fweightedrand/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mroth%2Fweightedrand/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mroth%2Fweightedrand/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mroth","download_url":"https://codeload.github.com/mroth/weightedrand/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254110374,"owners_count":22016391,"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":["go","golang","random","sampling","weighted-random"],"created_at":"2024-11-12T13:26:27.508Z","updated_at":"2025-05-14T09:06:28.849Z","avatar_url":"https://github.com/mroth.png","language":"Go","readme":"# weightedrand :balance_scale:\n\n[![PkgGoDev](https://pkg.go.dev/badge/github.com/mroth/weightedrand)](https://pkg.go.dev/github.com/mroth/weightedrand/v2)\n[![CodeFactor](https://www.codefactor.io/repository/github/mroth/weightedrand/badge)](https://www.codefactor.io/repository/github/mroth/weightedrand)\n[![Build Status](https://github.com/mroth/weightedrand/workflows/test/badge.svg)](https://github.com/mroth/weightedrand/actions)\n[![codecov](https://codecov.io/gh/mroth/weightedrand/graph/badge.svg?token=EOEE4E4Y0P)](https://codecov.io/gh/mroth/weightedrand)\n\n\u003e Fast weighted random selection for Go.\n\nRandomly selects an element from some kind of list, where the chances of each\nelement to be selected are not equal, but rather defined by relative \"weights\"\n(or probabilities). This is called weighted random selection.\n\n## Usage\n\n```go\nimport (\n    /* ...snip... */\n    \"github.com/mroth/weightedrand/v2\"\n)\n\nfunc main() {\n    chooser, _ := weightedrand.NewChooser(\n        weightedrand.NewChoice('🍒', 0),\n        weightedrand.NewChoice('🍋', 1),\n        weightedrand.NewChoice('🍊', 1),\n        weightedrand.NewChoice('🍉', 3),\n        weightedrand.NewChoice('🥑', 5),\n    )\n    // The following will print 🍋 and 🍊 with 0.1 probability, 🍉 with 0.3\n    // probability, and 🥑 with 0.5 probability. 🍒 will never be printed. (Note\n    // the weights don't have to add up to 10, that was just done here to make\n    // the example easier to read.)\n    result := chooser.Pick()\n    fmt.Println(result)\n}\n```\n\n## Performance\n\nThe existing Go library that has a comparable implementation of this is\n[`github.com/jmcvetta/randutil`][1], which optimizes for the single operation\ncase. In contrast, this library creates a presorted cache optimized for binary\nsearch, allowing repeated selections from the same set to be significantly\nfaster, especially for large data sets.\n\n[1]: https://github.com/jmcvetta/randutil\n\nComparison of this library versus `randutil.ChooseWeighted` on my workstation.\nFor repeated samplings from large collections, `weightedrand` will be much\nquicker:\n\n| Num choices |     `randutil` | `weightedrand` | `weightedrand -cpu=8`* |\n| ----------: | -------------: | -------------: | ---------------------: |\n|          10 |      201 ns/op |       38 ns/op |              2.9 ns/op |\n|         100 |      267 ns/op |       51 ns/op |              4.1 ns/op |\n|       1,000 |     1012 ns/op |       67 ns/op |              5.4 ns/op |\n|      10,000 |     8683 ns/op |       83 ns/op |              6.9 ns/op |\n|     100,000 |   123500 ns/op |      105 ns/op |             12.0 ns/op |\n|   1,000,000 |  2399614 ns/op |      218 ns/op |             17.2 ns/op |\n|  10,000,000 | 26804440 ns/op |      432 ns/op |             35.1 ns/op |\n\n**: Since `v0.3.0` weightedrand can efficiently utilize a single Chooser across\nmultiple CPU cores in parallel, making it even faster in overall throughput. See\n[PR#2](https://github.com/mroth/weightedrand/pull/2) for details. Informal\nbenchmarks conducted on an Intel Xeon W-2140B CPU (8 core @ 3.2GHz,\nhyperthreading enabled).*\n\nDon't be mislead by these numbers into thinking `weightedrand` is always the\nright choice! If you are only picking from the same distribution once,\n`randutil` will be faster. `weightedrand` optimizes for repeated calls at the\nexpense of some initialization time and memory storage.\n\n## Requirements\n\nweightedrand \u003e= v2 requires go1.18 or greater. For support on earlier versions\nof go, use weightedrand [v1](https://github.com/mroth/weightedrand/tree/v1).\n\n## Credits\n\nTo better understand the algorithm used in this library (as well as the one used\nin randutil) check out this great blog post: [Weighted random generation in Python](https://eli.thegreenplace.net/2010/01/22/weighted-random-generation-in-python/).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmroth%2Fweightedrand","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmroth%2Fweightedrand","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmroth%2Fweightedrand/lists"}