weightedrand

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 18, 2022 License: MIT Imports: 1 Imported by: 0

README

weightedrand

GoDoc CI codecov MIT License

A golang package for weighted random.

Usage

items := []weightedrand.WeightedItem[string]{
  {Weight: 5, Item: "Hi!"},
  {Weight: 3, Item: "Hello!"},
  {Weight: 2, Item: "What's up?"},
}

wr := weightedrand.New(time.Now().UnixNano(), items)

fmt.Println(wr.GetItem()) // "Hi!", "Hello!" or "What's up?"
Weight Item Expected Rate
5 "Hi!" 50 % (= 5 / (5 + 3 + 2))
3 "Hello!" 30 % (= 3 / (5 + 3 + 2))
2 "What's up?" 20 % (= 2 / (5 + 3 + 2))

Documentation

Overview

Package weightedrand implements weighted random selection. This package is intended for situations where you want to randomly generate bot comments, for example.

Note that at the moment, the algorithm is based on a simple cumulative sum, so the order of the selection is `O(N)`. The number of choice items should not be large, as it is not performance optimized.

Example
package main

import (
	"fmt"

	"github.com/takuoki/weightedrand"
)

func main() {

	items := []weightedrand.WeightedItem[string]{
		{Weight: 5, Item: "Hi!"},
		{Weight: 3, Item: "Hello!"},
		{Weight: 2, Item: "What's up?"},
	}

	// please set the appropriate seed like `time.Now().UnixNano()`.
	wr := weightedrand.New(1, items)

	for i := 0; i < 5; i++ {
		fmt.Println(wr.GetItem())
	}

}
Output:
Hi!
Hello!
Hello!
What's up?
Hi!
Example (CheckRate)
package main

import (
	"fmt"

	"github.com/takuoki/weightedrand"
)

func main() {

	const repeat = 100000

	items := []weightedrand.WeightedItem[int]{
		{Weight: 5, Item: 0},
		{Weight: 3, Item: 1},
		{Weight: 2, Item: 2},
	}

	wr := weightedrand.New(1, items)

	counts := make([]int, len(items))
	for i := 0; i < repeat; i++ {
		counts[wr.GetItem()]++
	}

	for i := 0; i < len(items); i++ {
		fmt.Printf("- %d: weight=%d, count=%d, rate=%.2f%%\n", i, items[i].Weight, counts[i], calcRate(counts[i], repeat))
	}

}

func calcRate(a, b int) float32 {
	return float32(a) / float32(b) * 100
}
Output:
- 0: weight=5, count=49965, rate=49.97%
- 1: weight=3, count=30295, rate=30.30%
- 2: weight=2, count=19740, rate=19.74%

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type WeightedItem

type WeightedItem[T any] struct {
	Weight int
	Item   T
}

WeightedItem is a weighted selection item. Any type can be used.

type WeightedRand

type WeightedRand[T any] struct {
	// contains filtered or unexported fields
}

WeightedRand is an object that performs weighted random selection. It should be created using the New function.

func New

func New[T any](seed int64, items []WeightedItem[T]) *WeightedRand[T]

New is a function that generates WeightedRand seeded with the given value.

func (*WeightedRand[T]) GetItem

func (r *WeightedRand[T]) GetItem() T

GetItem returns one of the selection items at random.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL