rand

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: GPL-3.0 Imports: 10 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 Rand

type Rand struct {
	// contains filtered or unexported fields
}

Rand wraps math/rand/v2's PCG generator and provides project-specific helpers (inclusive ranges, sampling, weighted sampling, etc.) that the stdlib does not. State serializes via PCG.MarshalBinary; we expose it as JSON and msgpack so a *Rand can ride along inside the persisted Sim.

func Make

func Make() *Rand

func (*Rand) AdjectiveNoun

func (r *Rand) AdjectiveNoun() string

func (*Rand) Bool

func (r *Rand) Bool() bool

func (*Rand) DurationRange added in v0.14.3

func (r *Rand) DurationRange(low, high time.Duration) time.Duration

func (*Rand) Float32

func (r *Rand) Float32() float32

func (*Rand) Float32Range added in v0.14.3

func (r *Rand) Float32Range(low, high float32) float32

func (*Rand) Int31n

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

func (*Rand) IntRange added in v0.14.3

func (r *Rand) IntRange(low, high int) int

IntRange returns a uniformly-sampled value in [low, high].

func (*Rand) Intn

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

func (*Rand) MarshalBinary added in v0.14.4

func (r *Rand) MarshalBinary() ([]byte, error)

func (*Rand) MarshalJSON added in v0.14.4

func (r *Rand) MarshalJSON() ([]byte, error)

func (*Rand) MarshalMsgpack added in v0.14.4

func (r *Rand) MarshalMsgpack() ([]byte, error)

func (*Rand) Seed

func (r *Rand) Seed(s uint64)

func (*Rand) Uint32

func (r *Rand) Uint32() uint32

func (*Rand) UnmarshalBinary added in v0.14.4

func (r *Rand) UnmarshalBinary(data []byte) error

func (*Rand) UnmarshalJSON added in v0.14.4

func (r *Rand) UnmarshalJSON(b []byte) error

func (*Rand) UnmarshalMsgpack added in v0.14.4

func (r *Rand) UnmarshalMsgpack(b []byte) error

Jump to

Keyboard shortcuts

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