std / rand

rand

import "std:rand"

Provides pseudo-random number generation using the xoshiro256** algorithm. State is seeded via SplitMix64 expansion. Includes both instance-based and global convenience APIs, plus typed Fisher-Yates shuffle functions.

View source on Codeberg →

Types

#
type Rand struct {
    s0 uint64
    s1 uint64
    s2 uint64
    s3 uint64
}

Rand holds the four 64-bit state words of the xoshiro256** pseudo-random number generator.

Constructor / Seeding

#
func NewRand(seed uint64) Rand

Creates a new Rand instance with its state initialized from the given seed using SplitMix64 expansion.

#
func Seed(r *Rand, seed uint64)

Re-seeds an existing Rand instance, resetting all four state words via SplitMix64 expansion from the given seed.

Core Generation

#
func Uint64(r *Rand) uint64

Returns a pseudo-random uint64 value from the full 64-bit range.

#
func Int63(r *Rand) int

Returns a non-negative pseudo-random int value in the range [0, 2^63).

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

Returns a non-negative pseudo-random int in the half-open interval [0, n). Panics if n <= 0.

#
func Float64(r *Rand) float64

Returns a pseudo-random float64 in the half-open interval [0.0, 1.0).

Shuffle

#
func ShuffleInts(r *Rand, arr []int)

Randomly shuffles a slice of ints in-place using the Fisher-Yates algorithm.

#
func ShuffleFloat64s(r *Rand, arr []float64)

Randomly shuffles a slice of float64s in-place using the Fisher-Yates algorithm.

#
func ShuffleStrings(r *Rand, arr []string)

Randomly shuffles a slice of strings in-place using the Fisher-Yates algorithm.

Global Seeding

#
func GlobalSeed(seed uint64)

Seeds the global pseudo-random number generator with the given value.

#
func GlobalSeedTime()

Seeds the global pseudo-random number generator using the current time. Convenient for non-reproducible sequences.

Global Generation

#
func GlobalUint64() uint64

Returns a pseudo-random uint64 from the global generator.

#
func GlobalInt63() int

Returns a non-negative pseudo-random int in the range [0, 2^63) from the global generator.

#
func GlobalIntn(n int) int

Returns a non-negative pseudo-random int in the half-open interval [0, n) from the global generator. Panics if n <= 0.

#
func GlobalFloat64() float64

Returns a pseudo-random float64 in the half-open interval [0.0, 1.0) from the global generator.

Global Shuffle

#
func GlobalShuffleInts(arr []int)

Randomly shuffles a slice of ints in-place using the global generator and the Fisher-Yates algorithm.

#
func GlobalShuffleFloat64s(arr []float64)

Randomly shuffles a slice of float64s in-place using the global generator and the Fisher-Yates algorithm.

#
func GlobalShuffleStrings(arr []string)

Randomly shuffles a slice of strings in-place using the global generator and the Fisher-Yates algorithm.