rand

package
v2.6.2 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LetterSeed           = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" // Upper- and lower-case letters
	LetterLowerSeed      = "abcdefghijklmnopqrstuvwxyz"                           // Lower-case letters
	LetterUpperSeed      = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"                           // Upper-case letters
	DigitSeed            = "0123456789"                                           // Digits
	DigitWithoutZeroSeed = "123456789"                                            // Digits without zero
	SymbolSeed           = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"                   // Special characters
)

Variables

This section is empty.

Functions

This section is empty.

Types

type PseudoRandom

type PseudoRandom struct{}

PseudoRandom is a pseudo-random number generator implemented on top of math/rand/v2.

It keeps no internal state and the global functions of math/rand/v2 are safe for concurrent use, so a PseudoRandom is safe to share across goroutines.

func (*PseudoRandom) ExpFloat64

func (*PseudoRandom) ExpFloat64() float64

ExpFloat64 returns a pseudo-random 64-bit float from an exponential distribution with rate parameter (lambda) 1 and mean 1.

func (*PseudoRandom) Float32

func (*PseudoRandom) Float32() float32

Float32 returns a pseudo-random 32-bit float in [0.0,1.0).

func (*PseudoRandom) Float64

func (*PseudoRandom) Float64() float64

Float64 returns a pseudo-random 64-bit float in [0.0,1.0).

func (*PseudoRandom) Int

func (*PseudoRandom) Int() int

Int returns a non-negative pseudo-random integer.

func (*PseudoRandom) Int32

func (*PseudoRandom) Int32() int32

Int32 returns a non-negative pseudo-random 31-bit integer.

func (*PseudoRandom) Int32N

func (*PseudoRandom) Int32N(n int32) int32

Int32N returns a non-negative pseudo-random 32-bit integer in [0,n). n must be greater than 0.

func (*PseudoRandom) Int64

func (*PseudoRandom) Int64() int64

Int64 returns a non-negative pseudo-random 63-bit integer.

func (*PseudoRandom) Int64N

func (*PseudoRandom) Int64N(n int64) int64

Int64N returns a non-negative pseudo-random 64-bit integer in [0,n). n must be greater than 0.

func (*PseudoRandom) IntN

func (*PseudoRandom) IntN(n int) int

IntN returns a non-negative pseudo-random integer in [0,n). n must be greater than 0.

func (*PseudoRandom) N

func (*PseudoRandom) N[Int intType](n Int) Int

N returns a pseudo-random integer in [0,n) for any integer type. n must be greater than 0.

func (*PseudoRandom) NormFloat64

func (*PseudoRandom) NormFloat64() float64

NormFloat64 returns a pseudo-random 64-bit float from the standard normal distribution (mean 0, standard deviation 1).

func (*PseudoRandom) Perm

func (*PseudoRandom) Perm(n int) []int

Perm returns a pseudo-random permutation of the integers in [0,n).

func (*PseudoRandom) Shuffle

func (*PseudoRandom) Shuffle(n int, swap func(i, j int))

Shuffle randomizes the order of the elements using swap. n is the number of elements and must not be negative.

func (*PseudoRandom) Uint

func (*PseudoRandom) Uint() uint

Uint returns a pseudo-random unsigned integer.

func (*PseudoRandom) Uint32

func (*PseudoRandom) Uint32() uint32

Uint32 returns a pseudo-random 32-bit unsigned integer.

func (*PseudoRandom) Uint32N

func (*PseudoRandom) Uint32N(n uint32) uint32

Uint32N returns a non-negative pseudo-random 32-bit unsigned integer in [0,n). n must be greater than 0.

func (*PseudoRandom) Uint64

func (*PseudoRandom) Uint64() uint64

Uint64 returns a pseudo-random 64-bit unsigned integer.

func (*PseudoRandom) Uint64N

func (*PseudoRandom) Uint64N(n uint64) uint64

Uint64N returns a non-negative pseudo-random 64-bit unsigned integer in [0,n). n must be greater than 0.

func (*PseudoRandom) UintN

func (*PseudoRandom) UintN(n uint) uint

UintN returns a non-negative pseudo-random unsigned integer in [0,n). n must be greater than 0.

type Rand

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

func NewRand

func NewRand(rng Random, safe ...bool) *Rand

NewRand returns a new random number generator backed by rng.

Passing true for safe enables concurrency safety by guarding rng with a mutex.

func (*Rand) Digits

func (r *Rand) Digits(length int, hasLeadingZero ...bool) string

Digits returns a random digit string of the given length. By default the first digit is not zero unless hasLeadingZero is true.

func (*Rand) Duration

func (r *Rand) Duration(min, max time.Duration) time.Duration

Duration returns a pseudo-random duration in [min,max].

func (*Rand) ExpFloat64

func (r *Rand) ExpFloat64() float64

ExpFloat64 returns a 64-bit float from an exponential distribution with rate parameter (lambda) 1 and mean 1.

func (*Rand) Float32

func (r *Rand) Float32() float32

Float32 returns a pseudo-random 32-bit float in [0.0,1.0).

func (*Rand) Float32R

func (rd *Rand) Float32R(min, max float32) float32

Float32R returns a pseudo-random 32-bit float in [min,max).

func (*Rand) Float64

func (r *Rand) Float64() float64

Float64 returns a pseudo-random 64-bit float in [0.0,1.0).

func (*Rand) Float64R

func (rd *Rand) Float64R(min, max float64) float64

Float64R returns a pseudo-random 64-bit float in [min,max).

func (*Rand) Int

func (r *Rand) Int() int

Int returns a non-negative pseudo-random integer.

func (*Rand) Int32

func (r *Rand) Int32() int32

Int32 returns a non-negative pseudo-random 31-bit integer.

func (*Rand) Int32N

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

Int32N returns a non-negative pseudo-random 32-bit integer in [0,n). n must be greater than 0.

func (*Rand) Int32R

func (rd *Rand) Int32R(min, max int32) int32

Int32R returns a pseudo-random 32-bit integer in [min,max].

func (*Rand) Int64

func (r *Rand) Int64() int64

Int64 returns a non-negative pseudo-random 63-bit integer.

func (*Rand) Int64N

func (r *Rand) Int64N(n int64) int64

Int64N returns a non-negative pseudo-random 64-bit integer in [0,n). n must be greater than 0.

func (*Rand) Int64R

func (rd *Rand) Int64R(min, max int64) int64

Int64R returns a pseudo-random 64-bit integer in [min,max].

func (*Rand) IntN

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

IntN returns a non-negative pseudo-random integer in [0,n). n must be greater than 0.

func (*Rand) IntR

func (rd *Rand) IntR(min, max int) int

IntR returns a pseudo-random integer in [min,max].

func (*Rand) Letters

func (r *Rand) Letters(length int) string

Letters returns a random string of the given length built from letters.

func (*Rand) Lucky

func (r *Rand) Lucky(probability float64, base ...float64) bool

Lucky reports whether a random draw hits the given probability.

func (*Rand) NormFloat64

func (r *Rand) NormFloat64() float64

NormFloat64 returns a 64-bit float from the standard normal distribution (mean 0, standard deviation 1).

func (*Rand) Perm

func (r *Rand) Perm(n int) []int

Perm returns a pseudo-random permutation of the integers in [0,n).

func (*Rand) Shuffle

func (r *Rand) Shuffle[T any](list []T)

Shuffle shuffles list in place.

func (*Rand) Str

func (r *Rand) Str(seed string, length int) string

Str returns a random string of the given length built from seed.

func (*Rand) Symbols

func (r *Rand) Symbols(length int) string

Symbols returns a random string of the given length built from special characters.

func (*Rand) Uint

func (r *Rand) Uint() uint

Uint returns a pseudo-random unsigned integer.

func (*Rand) Uint32

func (r *Rand) Uint32() uint32

Uint32 returns a pseudo-random 32-bit unsigned integer.

func (*Rand) Uint32N

func (r *Rand) Uint32N(n uint32) uint32

Uint32N returns a non-negative pseudo-random 32-bit unsigned integer in [0,n). n must be greater than 0.

func (*Rand) Uint64

func (r *Rand) Uint64() uint64

Uint64 returns a pseudo-random 64-bit unsigned integer.

func (*Rand) Uint64N

func (r *Rand) Uint64N(n uint64) uint64

Uint64N returns a non-negative pseudo-random 64-bit unsigned integer in [0,n). n must be greater than 0.

func (*Rand) UintN

func (r *Rand) UintN(n uint) uint

UintN returns a non-negative pseudo-random unsigned integer in [0,n). n must be greater than 0.

func (*Rand) Weight

func (rd *Rand) Weight[T any](fn func(v T) float64, list ...T) (int, T, bool)

Weight performs a weighted random draw over list, using fn to compute each element's weight.

It returns the index of the chosen element, the element itself and true, or -1, the zero value and false when no element is drawn. A draw fails when the total weight is not positive.

type Random

type Random interface {
	Int() int
	IntN(n int) int
	Int32() int32
	Int32N(n int32) int32
	Int64() int64
	Int64N(n int64) int64
	Uint() uint
	UintN(n uint) uint
	Uint32() uint32
	Uint32N(n uint32) uint32
	Uint64() uint64
	Uint64N(n uint64) uint64
	Float32() float32
	Float64() float64
	ExpFloat64() float64
	NormFloat64() float64
	Perm(n int) []int
	Shuffle(n int, swap func(i, j int))
}

type TrueRandom

type TrueRandom struct{}

TrueRandom is a true random number generator implemented on top of crypto/rand.

It keeps no internal state and the underlying crypto/rand.Read is safe for concurrent use, so a TrueRandom is safe to share across goroutines.

func (*TrueRandom) ExpFloat64

func (r *TrueRandom) ExpFloat64() float64

ExpFloat64 returns a true random 64-bit float from an exponential distribution with rate parameter (lambda) 1 and mean 1, generated by inverse transform sampling.

func (*TrueRandom) Float32

func (r *TrueRandom) Float32() float32

Float32 returns a true random 32-bit float in [0.0,1.0).

func (*TrueRandom) Float64

func (r *TrueRandom) Float64() float64

Float64 returns a true random 64-bit float in [0.0,1.0).

func (*TrueRandom) Int

func (r *TrueRandom) Int() int

Int returns a non-negative true random integer.

func (*TrueRandom) Int32

func (r *TrueRandom) Int32() int32

Int32 returns a non-negative true random 31-bit integer.

func (*TrueRandom) Int32N

func (r *TrueRandom) Int32N(n int32) int32

Int32N returns a non-negative true random 32-bit integer in [0,n). n must be greater than 0.

func (*TrueRandom) Int64

func (r *TrueRandom) Int64() int64

Int64 returns a non-negative true random 63-bit integer.

func (*TrueRandom) Int64N

func (r *TrueRandom) Int64N(n int64) int64

Int64N returns a non-negative true random 64-bit integer in [0,n). n must be greater than 0.

func (*TrueRandom) IntN

func (r *TrueRandom) IntN(n int) int

IntN returns a non-negative true random integer in [0,n). n must be greater than 0.

func (*TrueRandom) N

func (r *TrueRandom) N[Int intType](n Int) Int

N returns a true random integer in [0,n) for any integer type. n must be greater than 0.

func (*TrueRandom) NormFloat64

func (r *TrueRandom) NormFloat64() float64

NormFloat64 returns a true random 64-bit float from the standard normal distribution (mean 0, standard deviation 1), generated by transforming a uniform distribution with the Box-Muller method.

func (*TrueRandom) Perm

func (r *TrueRandom) Perm(n int) []int

Perm returns a true random permutation of the integers in [0,n).

func (*TrueRandom) Shuffle

func (r *TrueRandom) Shuffle(n int, swap func(i, j int))

Shuffle randomizes the order of the elements using swap. n is the number of elements and must not be negative.

func (*TrueRandom) Uint

func (r *TrueRandom) Uint() uint

Uint returns a true random unsigned integer.

func (*TrueRandom) Uint32

func (r *TrueRandom) Uint32() uint32

Uint32 returns a true random 32-bit unsigned integer.

func (*TrueRandom) Uint32N

func (r *TrueRandom) Uint32N(n uint32) uint32

Uint32N returns a non-negative true random 32-bit unsigned integer in [0,n). n must be greater than 0.

func (*TrueRandom) Uint64

func (r *TrueRandom) Uint64() uint64

Uint64 returns a true random 64-bit unsigned integer.

func (*TrueRandom) Uint64N

func (r *TrueRandom) Uint64N(n uint64) uint64

Uint64N returns a non-negative true random 64-bit unsigned integer in [0,n). n must be greater than 0.

func (*TrueRandom) UintN

func (r *TrueRandom) UintN(n uint) uint

UintN returns a non-negative true random unsigned integer in [0,n). n must be greater than 0.

Jump to

Keyboard shortcuts

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