Documentation
¶
Index ¶
- Variables
- func N[Int intType](n Int) Int
- func Num[Number numericType]() Number
- func Pick[E any](in []E) E
- func PickN[E any](in []E, n int) []E
- func PickNDistinct[E any](slice []E, n int) []E
- func PickNUnique[S ~[]E, E comparable](slice S, n int) (S, error)
- func Range[T numericType](min, max T) T
- func ReplaceRandSource(src rand.Source)
- func Shuffle[T any](in []T)
- type Lottery
- func (l *Lottery[T]) Append(values ...T) *Lottery[T]
- func (l *Lottery[T]) AppendWeight(weight float64, values ...T) *Lottery[T]
- func (l *Lottery[T]) AppendWeights(items map[float64][]T) *Lottery[T]
- func (l *Lottery[T]) Clear()
- func (l *Lottery[T]) Draw() T
- func (l *Lottery[T]) DrawN(n int) []T
- func (l *Lottery[T]) Items() []lotteryItem[T]
- func (l *Lottery[T]) Size() int
- type Probability
Constants ¶
This section is empty.
Variables ¶
var P50 = Probability(0.5)
Functions ¶
func N ¶
func N[Int intType](n Int) Int
N returns a non-negative pseudo-random number in the range [0, n]. It panics if n <= 0.
func Num ¶
func Num[Number numericType]() Number
Num generates a random number of the specified numeric type.
For floating-point types (float32, float64), it returns a random value between 0.0 and 1.0. For integer types, it returns a random value within the full range of that type.
func Pick ¶
func Pick[E any](in []E) E
Pick returns a random element from the provided slice. If the slice is empty, it returns the zero value of type E.
func PickN ¶
PickN returns n randomly selected elements from the input slice.
Elements may be duplicated if n is greater than the length of the input slice. Returns nil if n is less than or equal to 0 or if the input slice is empty. The selection uses a random permutation to determine which elements to pick.
func PickNDistinct ¶
PickNDistinct returns n distinct elements randomly selected from the given slice.
The function uses Fisher-Yates shuffle algorithm to ensure each element has an equal probability of being selected. The order of elements in the returned slice is randomized. Returns nil if n <= 0. Panics if n is greater than the length of the input slice.
func PickNUnique ¶
func PickNUnique[S ~[]E, E comparable](slice S, n int) (S, error)
PickNUnique returns n randomly selected unique elements from the given slice.
It first removes duplicates from the input slice, then randomly picks n distinct elements. Returns an error if n is less than or equal to 0, or if n exceeds the number of unique elements in the slice.
func Range ¶
func Range[T numericType](min, max T) T
Range generates a random number of type T within the specified range [min, max]. It panics if min < 0 or max <= 0.
func ReplaceRandSource ¶
ReplaceRandSource replaces the global random number generator's source with the provided source.
This function allows for customizing the randomization behavior by using a different source implementation, such as for testing with deterministic seeds or using alternative random number generation algorithms.
Types ¶
type Lottery ¶ added in v0.0.2
type Lottery[T any] struct { // contains filtered or unexported fields }
Lottery is a thread-safe generic lottery system that allows weighted random selection of items of type T. It maintains a collection of lottery items, each with an associated weight that determines the probability of selection.
func NewLottery ¶ added in v0.0.2
NewLottery creates a new Lottery instance with the provided initial items.
func (*Lottery[T]) Append ¶ added in v0.0.2
Append adds one or more values to the lottery with a default weight of 1.
func (*Lottery[T]) AppendWeight ¶ added in v0.0.4
AppendWeight adds one or more values to the lottery with the specified weight. The weight determines the probability of each value being selected during a draw. Higher weights increase the likelihood of selection. weight <= 0 means the item will never be drawn.
func (*Lottery[T]) AppendWeights ¶ added in v0.0.4
AppendWeights adds multiple items to the lottery with their associated weights.
func (*Lottery[T]) Clear ¶ added in v0.0.2
func (l *Lottery[T]) Clear()
Clear removes all items from the lottery, making it empty.
func (*Lottery[T]) Draw ¶ added in v0.0.2
func (l *Lottery[T]) Draw() T
Draw selects and returns a random item from the lottery based on weighted probabilities.
If the lottery is empty, it returns the zero value of type T. Items with weight <= 0 are excluded from the weighted selection but may still be returned as a fallback if no weighted items exist.
The selection algorithm uses cumulative weight distribution for fair randomization.
type Probability ¶
type Probability float64
Probability represents a probability value between 0 and 1 (inclusive).
func (Probability) Check ¶
func (p Probability) Check() bool
Check returns true with the probability specified by p.
If p is less than or equal to 0, it always returns false. If p is greater than or equal to 1, it always returns true. For values between 0 and 1, it returns true with probability p using a random number generator.