random

package
v1.14.5 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const K = 0x9e3779b97f4a7c13

proportionality factor

Variables

This section is empty.

Functions

func Range added in v1.14.4

func Range[T INumber](rng *rand.Rand, min, max T) T

Generate a random integer between min and max (inclusive) If min is greater than max, the values are swapped. This function is not thread-safe, so it should be used in a single goroutine or protected by a mutex.

func Slices added in v1.14.4

func Slices[T comparable, S ~[]T](rng *rand.Rand, s S, count int) S

Slices returns a slice of random elements from the input slice s. The number of elements in the result slice is equal to count.

Types

type INumber added in v1.14.4

type INumber interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64
}

Define the types that can be used with RangeRandom The ~ operator allows for type sets that include all types that are assignable to the specified type. This includes all integer types, unsigned integer types, and floating-point types.

type WeightItem

type WeightItem[T any, WT INumber] struct {
	Value  T
	Weight WT
}

WeightItem is a weighted item.

type WeightPool

type WeightPool[T any, WT INumber] struct {
	// contains filtered or unexported fields
}

WeightPool is a weighted random pool. It is used to select a random item from a list of items with different weights.

func NewWeightPool

func NewWeightPool[T any, WT INumber](items []WeightItem[T, WT]) *WeightPool[T, WT]

func (*WeightPool[T, WT]) Random

func (p *WeightPool[T, WT]) Random() T

Select a random item from the pool based on its weight. The probability of selecting an item is proportional to its weight. The higher the weight, the more likely it is to be selected.

func (*WeightPool[T, WT]) WithRngSource added in v1.14.4

func (p *WeightPool[T, WT]) WithRngSource(src rand.Source)

WithRngSource .

Jump to

Keyboard shortcuts

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