sergonie / wrand

Lightweight golang library for weighted random selection.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

WRAND

Go

Lightweight golang library for obtaining a weighted random item.

Installation

go get -u github.com/sergonie/wrand

Example

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)
}

About

Lightweight golang library for weighted random selection.

License:MIT License


Languages

Language:Go 100.0%