weightedrand

package
v0.0.0-...-32efd3b Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2025 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendWeightedRandomChoices

func AppendWeightedRandomChoices[T any](choices []Choice[T], result []*T) ([]*T, error)

AppendWeightedRandomChoices appends weighted randomized elements to a slice. The order of previously added elements in the result list is retained

func CreateWeightedRandomList

func CreateWeightedRandomList[T any](source []*T) ([]*T, error)

CreateWeightedRandomList creates weighted randomized slice containing all elements of the source slice

Types

type Choice

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

Choice is a generic wrapper that can be used to add weights for any item.

type Chooser

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

A Chooser caches many possible Choices in a structure designed to improve performance on repeated calls for weighted random selection.

func NewChooser

func NewChooser[T any](choices ...Choice[T]) (*Chooser[T], error)

NewChooser initializes a new Chooser for picking from the provided choices.

func (*Chooser[T]) PickRandom

func (c *Chooser[T]) PickRandom() (*T, error)

PickRandom removes a random item from the list based on weights and returns it. This method is not thread safe.

Jump to

Keyboard shortcuts

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