https://github.com/sergonie/wrand
Lightweight golang library for weighted random selection.
https://github.com/sergonie/wrand
fast go golang random weighted
Last synced: 6 months ago
JSON representation
Lightweight golang library for weighted random selection.
- Host: GitHub
- URL: https://github.com/sergonie/wrand
- Owner: sergonie
- License: mit
- Created: 2021-04-26T20:38:55.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-04-26T21:49:33.000Z (about 5 years ago)
- Last Synced: 2024-06-21T09:46:01.394Z (about 2 years ago)
- Topics: fast, go, golang, random, weighted
- Language: Go
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# WRAND
[](https://github.com/sergonie/wrand/actions/workflows/go.yml)
Lightweight golang library for obtaining a weighted random item.
## Installation
```shell
go get -u github.com/sergonie/wrand
```
## Example
```go
package main
import (
"fmt"
"github.com/sergonie/wrand"
)
func main() {
myDataset := []struct {
Name string
Weight int
}{
{
Name: "Max",
Weight: 80,
},
{
Name: "El",
Weight: 110,
},
{
Name: "Gabriella",
Weight: 45,
},
}
items := &wrand.ItemsCollection{}
for k, v := range myDataset {
items.Add(k, v.Weight)
}
selectedItem := wrand.NewPicker().Pick(items)
fmt.Printf("Winner: %s\n", myDataset[selectedItem.Value].Name)
}
```