rng

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2025 License: MIT Imports: 3 Imported by: 0

README

rng

Randomness Goodies

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

func PickN[E any](in []E, n int) []E

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

func PickNDistinct[E any](slice []E, n int) []E

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

func ReplaceRandSource(src rand.Source)

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.

func Shuffle

func Shuffle[T any](in []T)

Shuffle randomly rearranges the elements of the slice in place using the Fisher-Yates shuffle algorithm.

The function modifies the original slice and does not return a new slice. If the slice has 1 or fewer elements, no shuffling is performed.

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

func NewLottery[T any](items ...T) *Lottery[T]

NewLottery creates a new Lottery instance with the provided initial items.

func (*Lottery[T]) Append added in v0.0.2

func (l *Lottery[T]) Append(values ...T) *Lottery[T]

Append adds one or more values to the lottery with a default weight of 1.

func (*Lottery[T]) AppendWeight added in v0.0.4

func (l *Lottery[T]) AppendWeight(weight float64, values ...T) *Lottery[T]

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

func (l *Lottery[T]) AppendWeights(items map[float64][]T) *Lottery[T]

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.

func (*Lottery[T]) DrawN added in v0.0.3

func (l *Lottery[T]) DrawN(n int) []T

func (*Lottery[T]) Items added in v0.0.2

func (l *Lottery[T]) Items() []lotteryItem[T]

Items returns a copy of all lottery items with their associated weights and values.

func (*Lottery[T]) Size added in v0.0.2

func (l *Lottery[T]) Size() int

Size returns the number of items currently in the lottery.

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.

Directories

Path Synopsis
examples
lottery command
rng command

Jump to

Keyboard shortcuts

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