weightedoption

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2024 License: MIT Imports: 4 Imported by: 0

README

weightedoption

A Go package for weighted random option selection

Example Usage

package main

import (
	"fmt"
	"log"

	"github.com/eljamo/weightedoption"
)

// Simulates 100 chances for dropping a raid exotic weapon from a Destiny which has a 5% drop chance when a player completes the raid
func main() {
	s, err := weightedoption.NewSelector(
		weightedoption.NewOption('🔫', 5),
		weightedoption.NewOption('❌', 95),
	)
	if err != nil {
		log.Fatal(err)
	}

	chances := make([]rune, 30)
	for i := 0; i < len(chances); i++ {
		chances[i] = s.Select()
	}
	fmt.Println(string(chances))

	tally := make(map[rune]int)
	for _, c := range chances {
		tally[c]++
	}

	_, err = fmt.Printf("\n🔫: %d\t❌ %d\n", tally['🔫'], tally['❌'])
	if err != nil {
		log.Fatal(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrWeightOverflow = errors.New("sum of Option weights exceeds total")
	ErrNoValidOptions = errors.New("0 Option(s) with Weight >= 1")
)

Functions

This section is empty.

Types

type Option

type Option[DataType any, WeightIntegerType WeightIntegerConstraint] struct {
	Data   DataType
	Weight WeightIntegerType
}

Option is a struct that holds a data value and its associated weight.

func NewOption

func NewOption[DataType any, WeightIntegerType WeightIntegerConstraint](
	data DataType,
	weight WeightIntegerType,
) Option[DataType, WeightIntegerType]

NewOption creates a new Option.

type SearchIntsFuncSignature

type SearchIntsFuncSignature func(runningTotalWeights []int, randInt int) int

SearchIntsFuncSignature is the signature of the function used to search for an integer in a sorted slice of integers.

type Selector

type Selector[DataType any, WeightIntegerType WeightIntegerConstraint] struct {
	// contains filtered or unexported fields
}

Selector is a struct that holds a slice of Options and their cumulative weights.

func NewSelector

func NewSelector[DataType any, WeightIntegerType WeightIntegerConstraint](
	options ...Option[DataType, WeightIntegerType],
) (*Selector[DataType, WeightIntegerType], error)

NewSelector creates a new Selector.

func NewSelectorUsingSortSearchInts

func NewSelectorUsingSortSearchInts[DataType any, WeightIntegerType WeightIntegerConstraint](
	options ...Option[DataType, WeightIntegerType],
) (*Selector[DataType, WeightIntegerType], error)

NewSelectorUsingSortSearchInts creates a new Selector using the sort.SearchInts function.

func NewSelectorWithCustomSearchIntsFunc

func NewSelectorWithCustomSearchIntsFunc[DataType any, WeightIntegerType WeightIntegerConstraint](
	searchIntsFunc SearchIntsFuncSignature,
	options ...Option[DataType, WeightIntegerType],
) (*Selector[DataType, WeightIntegerType], error)

NewSelectorWithCustomSearchIntsFunc creates a new Selector with a custom searchIntsFunc.

func (Selector[DataType, WeightIntegerType]) Select

func (s Selector[DataType, WeightIntegerType]) Select() DataType

Select returns a single option from the Selector.

type WeightIntegerConstraint

type WeightIntegerConstraint interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

WeightIntegerConstraint is a type constraint for the Weight field of the Option struct.

Jump to

Keyboard shortcuts

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