selector

package
v1.16.2 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

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 builds a selector that returns values in order.

The selector keeps the original slice and advances an internal index on each Pick call until all entries have been exhausted.

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 builds a destructive weighted selector over values.

The values and weights slices are copied so callers can safely reuse or modify their inputs after the call. Each Pick removes the chosen entry from future draws, which matches fanout's per-request weighted selection model.

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.

Jump to

Keyboard shortcuts

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