Documentation
¶
Overview ¶
Package selector implements weighted random selection algorithm
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Sequential ¶
type Sequential[T any] struct { // contains filtered or unexported fields }
Sequential acts like a queue and picks elements one-by-one starting from the first element.
func NewSequentialSelector ¶
func NewSequentialSelector[T any](values []T) *Sequential[T]
NewSequentialSelector initializes a Sequential selector with default starting index 0.
func (*Sequential[T]) Pick ¶
func (s *Sequential[T]) Pick() T
Pick returns the next available element from the values array if one exists. Returns the zero value of type T otherwise.
type WeightedRand ¶
type WeightedRand[T any] struct { // contains filtered or unexported fields }
WeightedRand picks elements randomly based on their weights. It uses math/rand/v2 global functions which are safe for concurrent use.
func NewWeightedRandSelector ¶
func NewWeightedRandSelector[T any](values []T, weights []int) *WeightedRand[T]
NewWeightedRandSelector initializes a WeightedRand by copying source values and calculating total weight.
func (*WeightedRand[T]) Pick ¶
func (wrs *WeightedRand[T]) Pick() T
Pick returns a randomly chosen element from values based on its weight, if any exists.