wrand

package
v1.1.23 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ShuffleWithWeights

func ShuffleWithWeights(weights []int) ([]int, error)

Types

type Choice

type Choice struct {
	Key    any
	Item   any
	Weight uint
}

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

func NewChoice

func NewChoice(key any, item any, weight uint) Choice

NewChoice creates a new Choice with specified item and weight.

type Chooser

type Chooser 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(choices ...Choice) (*Chooser, error)

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

func NewChooserWithWeights

func NewChooserWithWeights(items []int, weights []int) (*Chooser, error)

func (Chooser) Pick

func (c Chooser) Pick() (key any, item interface{})

Pick returns a single weighted random Choice.Item from the Chooser.

Utilizes global rand as the source of randomness.

Jump to

Keyboard shortcuts

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