rand

package
v0.13.3 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2025 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PermutationElement

func PermutationElement(i int, n int, p uint32) int

PermutationElement returns the ith element of a random permutation of the set of integers [0...,n-1]. i/n, p is hash, via Andrew Kensler

func PermuteSlice

func PermuteSlice[Slice ~[]E, E any](s Slice, seed uint32) iter.Seq2[int, E]

func Sample

func Sample[T any](r *Rand, t ...T) T

func SampleFiltered

func SampleFiltered[T any](r *Rand, slice []T, pred func(T) bool) int

SampleFiltered uniformly randomly samples a slice, returning the index of the sampled item, using provided predicate function to filter the items that may be sampled. An index of -1 is returned if the slice is empty or the predicate returns false for all items.

func SampleSeq

func SampleSeq[T any](r *Rand, it iter.Seq[T]) (sample T, ok bool)

func SampleSlice

func SampleSlice[T any](r *Rand, slice []T) T

SampleSlice uniformly randomly samples an element of a non-empty slice.

func SampleWeighted

func SampleWeighted[T any, W constraints.Integer | constraints.Float](r *Rand, slice []T, weight func(T) W) (T, bool)

SampleWeighted randomly samples an element from the given slice with the probability of choosing each element proportional to the value returned by the provided callback.

func SampleWeightedSeq

func SampleWeightedSeq[T any, W constraints.Integer | constraints.Float](r *Rand, it iter.Seq[T], weight func(T) W) (sample T, ok bool)

func ShuffleSlice

func ShuffleSlice[Slice ~[]E, E any](s Slice, r *Rand)

Types

type PCG32

type PCG32 struct {
	State     uint64
	Increment uint64
}

func NewPCG32

func NewPCG32() PCG32

func (*PCG32) Bounded

func (p *PCG32) Bounded(bound uint32) uint32

func (*PCG32) Random

func (p *PCG32) Random() uint32

func (*PCG32) Seed

func (p *PCG32) Seed(state, sequence uint64)

type Rand

type Rand struct {
	PCG32
}

func Make

func Make() *Rand

func (*Rand) AdjectiveNoun

func (r *Rand) AdjectiveNoun() string

func (*Rand) Bool

func (r *Rand) Bool() bool

func (*Rand) Float32

func (r *Rand) Float32() float32

func (*Rand) Int31n

func (r *Rand) Int31n(n int32) int32

func (*Rand) Intn

func (r *Rand) Intn(n int) int

func (*Rand) Seed

func (r *Rand) Seed(s uint64)

func (*Rand) Uint32

func (r *Rand) Uint32() uint32

Jump to

Keyboard shortcuts

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