{"id":40162253,"url":"https://github.com/minaguib/weightedrandom","last_synced_at":"2026-01-19T16:30:42.828Z","repository":{"id":57521731,"uuid":"235808888","full_name":"minaguib/weightedrandom","owner":"minaguib","description":"A Go library for efficient weighted random picking","archived":false,"fork":false,"pushed_at":"2022-03-04T23:44:55.000Z","size":15,"stargazers_count":13,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-15T16:39:14.158Z","etag":null,"topics":["alias","go","golang","random","vose","weighted"],"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/minaguib.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}},"created_at":"2020-01-23T14:14:18.000Z","updated_at":"2025-07-26T19:18:57.000Z","dependencies_parsed_at":"2022-09-26T18:01:15.803Z","dependency_job_id":null,"html_url":"https://github.com/minaguib/weightedrandom","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/minaguib/weightedrandom","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/minaguib%2Fweightedrandom","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/minaguib%2Fweightedrandom/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/minaguib%2Fweightedrandom/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/minaguib%2Fweightedrandom/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/minaguib","download_url":"https://codeload.github.com/minaguib/weightedrandom/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/minaguib%2Fweightedrandom/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28574215,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-19T16:29:19.148Z","status":"ssl_error","status_checked_at":"2026-01-19T16:29:17.772Z","response_time":67,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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","go","golang","random","vose","weighted"],"created_at":"2026-01-19T16:30:42.764Z","updated_at":"2026-01-19T16:30:42.815Z","avatar_url":"https://github.com/minaguib.png","language":"Go","readme":"# minaguib/weightedrandom\n\nA Go (golang) library for efficient weighted random picking\n\n[![GoDoc](https://godoc.org/github.com/minaguib/weightedrandom?status.svg)](https://godoc.org/github.com/minaguib/weightedrandom)\n[![Build Status](https://travis-ci.org/minaguib/weightedrandom.svg)](https://travis-ci.org/minaguib/weightedrandom)\n[![Coverage Status](https://coveralls.io/repos/github/minaguib/weightedrandom/badge.svg?branch=master)](https://coveralls.io/github/minaguib/weightedrandom?branch=master)\n[![GoReport](https://goreportcard.com/badge/github.com/minaguib/weightedrandom)](https://goreportcard.com/report/github.com/minaguib/weightedrandom)\n\n## About\n\nGiven a theoretical jar of 1,000 marbles, like so:\n\n|Marble color|Count|\n|---|---|\n|Red|500|\n|Blue|250|\n|Green|125|\n|Yellow|120|\n|Transparent|4|\n|Vantablack|1|\n\nYou want a solution that allows you to simulate picking a random marble while adhering to the above probabilities.  You want it to operate as fast as possible, require as little storage as possible, and efficiently handle large weights and large number of items.\n\n\n## About the implementation\n\nThis library implements the Alias picking method as further optimized by Michael D. Vose\n\nRef. Paper: \"[A Linear Algorithm For Generating Random Numbers With a Given Distribution](https://pdfs.semanticscholar.org/f65b/cde1fcf82e05388b31de80cba10bf65acc07.pdf)\" by Michael D. Vose\n\nRef: [Darts, Dice, and Coins: Sampling from a Discrete Distribution](https://www.keithschwarz.com/darts-dice-coins/) by Keith Schwarz\n\n## Performance characteristics\n\n|Item|Cost|\n|---|---|\n|Time to initialize|O(n)|\n|Data structure storage|O(n)|\n|Time to pick an item|O(1)|\n\n## Performance benchmarks\n\nOn a MacBook Pro, Intel(R) Core(TM) i5-8279U CPU @ 2.40GHz, 2133 MHz LPDDR3 memory:\n\n```\n$ go test -bench .\ngoos: darwin\ngoarch: amd64\npkg: github.com/minaguib/weightedrandom\n\nBenchmarkNew/1_weights-8           126366      8967 ns/op\nBenchmarkNew/10_weights-8          126315      9015 ns/op\nBenchmarkNew/100_weights-8         112978      9985 ns/op\nBenchmarkNew/1000_weights-8         58854     19891 ns/op\nBenchmarkNew/10000_weights-8         9981    109303 ns/op\nBenchmarkNew/100000_weights-8        1029   1001077 ns/op\nBenchmarkNew/1000000_weights-8        112   9697761 ns/op\nBenchmarkNew/10000000_weights-8        12  89310961 ns/op\n\nBenchmarkPick/from_1_weights-8            76958226        13.8 ns/op\nBenchmarkPick/from_10_weights-8           42556176        26.2 ns/op\nBenchmarkPick/from_100_weights-8          43152367        26.0 ns/op\nBenchmarkPick/from_1000_weights-8         42600124        26.0 ns/op\nBenchmarkPick/from_10000_weights-8        39454466        27.1 ns/op\nBenchmarkPick/from_100000_weights-8       31104279        32.5 ns/op\nBenchmarkPick/from_1000000_weights-8      17665467        69.6 ns/op\nBenchmarkPick/from_10000000_weights-8     11770742       104 ns/op\n```\n\n## Usage\n\nFor usage, examples and documentation, see [GoDoc](https://godoc.org/github.com/minaguib/weightedrandom)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fminaguib%2Fweightedrandom","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fminaguib%2Fweightedrandom","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fminaguib%2Fweightedrandom/lists"}