Documentation
¶
Overview ¶
package random provides cryptographic sourced random utility functions.
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrChoiceIsEmpty = errors.New("can't choose from an empty slice")
ErrChoiceIsEmpty is returned if the user attempts to choose from an empty slice.
Functions ¶
func Selection ¶ added in v1.2.0
Selection returns a slice of 'n' random entries from the given slice.
NOTE: This should not be used for password generation.
func WeightedChoice ¶
func WeightedChoice[T any](s []WeightedChoiceOption[T]) (T, error)
WeightedChoice returns an element from the given slice of options where elements with a higher weight are more likely to be selected.
Types ¶
type WeightedChoiceOption ¶
type WeightedChoiceOption[T any] struct { // Weight of the option, a higher weight means it's more likely to be selected. // // NOTE: The sum of the overall weights must be less than 'math.MaxInt'. Weight uint // Option that may be picked. Option T }
WeightedChoiceOption pairs a type with a weight.
Click to show internal directories.
Click to hide internal directories.