Documentation ¶
Overview ¶
Package weightedrand contains a performant data structure and algorithm used to randomly select an element from some kind of list, where the chances of each element to be selected not being equal, but defined by relative "weights" (or probabilities). This is called weighted random selection.
There is an existing Go library that has a generic implementation of this as github.com/jmcvetta/randutil, which optimizes for the single operation case. In contrast, this package creates a presorted cache optimized for binary search, allowing repeated selections from the same set to be significantly faster, especially for large data sets.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Choice ¶
type Choice struct { Item interface{} Weight uint }
Choice is a generic wrapper that can be used to add weights for any object
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 ¶
NewChooser initializes a new Chooser consisting of the possible Choices.