An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

# WRAND

[![Go](https://github.com/sergonie/wrand/actions/workflows/go.yml/badge.svg?branch=master)](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)
}
```