random

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2026 License: AGPL-3.0 Imports: 3 Imported by: 3

Documentation

Overview

Package random contains various methods for creating random values.

Package random contains various methods for creating random values.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Angle

func Angle() float64

Angle returns a random angle from 0 to 2PI radians.

func Around

func Around(val, amt float64) float64

Around returns a random float around a given number. val is the center. amt is how much on either side the result will vary.

func Bit added in v0.7.2

func Bit() int

Bit returns a random 1 or 0

func Boolean

func Boolean() bool

Boolean returns a random boolean.

func Byte added in v0.7.2

func Byte() byte

Byte returns a random byte [0, 255]

func Choice added in v0.9.0

func Choice[T any](list ...T) T

Choice returns a random choice from the parameters passed in.

func Complex added in v1.0.1

func Complex() complex128

Complex returns a random complex number from -1 + -1i to 1 + 1i

func ComplexRange added in v1.0.1

func ComplexRange(realMin, realMax, imagMin, imagMax float64) complex128

ComplexRange returns a random complex number in the given ranges.

func Element

func Element[T any](list []T) T

Element returns a random element from the list. Can't make this an instance method because generic methods cannot contain typed params and the receiver is actually a type param.

func Float

func Float() float64

Float returns a random float from 0.0 to 1.0.

func FloatArray

func FloatArray(size int, minVal, maxVal float64) []float64

FloatArray returns an array of a given size filled with random floats from minVal to maxVal.

func FloatMax added in v1.0.1

func FloatMax(maxVal float64) float64

FloatMax returns a random float from 0 to maxVal.

func FloatRange

func FloatRange(minVal float64, maxVal float64) float64

FloatRange returns a random float from minVal to maxVal.

func Halton added in v0.7.2

func Halton(base, index int) float64

Halton returns a quasirandom number from the Halton sequence.

func HaltonSequence added in v0.7.2

func HaltonSequence(base, start, count int) []float64

HaltonSequence returns a slice of floats from the Halton sequence, starting at start.

func Int

func Int() int

Int returns a random integer.

func IntArray

func IntArray(size int, minVal, maxVal int) []int

IntArray returns an array of a given size filled with random int from minVal to maxVal.

func IntMax added in v1.0.1

func IntMax(maxVal int) int

IntMax returns a random int from 0 to maxVal.

func IntRange

func IntRange(minVal int, maxVal int) int

IntRange returns a random int from minVal to maxVal.

func Norm

func Norm(mean, std float64) float64

Norm returns a random number within a normal distribution with the given mean and standard deviation.

func NormRange added in v1.0.1

func NormRange(minVal, maxVal, std float64) float64

NormRange returns a random number within a normal distribution (mostly) within a minVal/maxVal range. std is the standard deviation. Something around 0.2 will put almost all the value within the range. Higher values will spread it out more so that a larger percentage of values will be out of the range.

func Power

func Power(minVal, maxVal, power float64) float64

Power returns a random number raised to a power.

func RandSeed

func RandSeed()

RandSeed seeds the prng with a random seed.

func Seed

func Seed(seed uint64)

Seed sets the prng seed.

func Sign

func Sign() float64

Sign returns either -1.0 or 1.0 with equal distribution.

func String

func String(length int) string

String returns a random string.

func StringAlpha

func StringAlpha(length int) string

StringAlpha returns a random string of letters.

func StringFrom added in v1.0.1

func StringFrom(length int, chars string) string

StringFrom returns a random string of characters from the source string.

func StringLower

func StringLower(length int) string

StringLower returns a random lower case string.

func StringUpper

func StringUpper(length int) string

StringUpper returns a random upper case string.

func WeightedBool

func WeightedBool(weight float64) bool

WeightedBool returns a weighted boolean.

func WeightedElement added in v0.9.0

func WeightedElement[T any](list []T, weights []float64) T

WeightedElement returns a random element from the list, weighted per the weights param.

func WeightedIndex added in v1.0.1

func WeightedIndex(weights []float64) int

WeightedIndex returns a random int based on an array of weights.

Types

type Random

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

Random represents a single unique PRNG

func Default added in v1.0.1

func Default() *Random

Default ...

func NewRandom

func NewRandom() *Random

NewRandom creates a new Random instance.

func (*Random) Angle

func (r *Random) Angle() float64

Angle returns a random angle from 0 to 2PI radians.

func (*Random) Boolean

func (r *Random) Boolean() bool

Boolean returns a random boolean.

func (Random) Complex

func (r Random) Complex() complex128

Complex returns a random complex number from -1 + -1i to 1 + 1i

func (Random) ComplexRange

func (r Random) ComplexRange(realMin, realMax, imagMin, imagMax float64) complex128

ComplexRange returns a random complex number in the given ranges.

func (*Random) Float

func (r *Random) Float() float64

Float returns a random float from 0.0 to 1.0.

func (*Random) FloatArray

func (r *Random) FloatArray(size int, minVal, maxVal float64) []float64

FloatArray returns an array of a given size filled with random floats from minVal to maxVal.

func (*Random) FloatMax added in v1.0.1

func (r *Random) FloatMax(maxVal float64) float64

FloatMax returns a random float from 0 to maxVal.

func (*Random) FloatRange

func (r *Random) FloatRange(minVal float64, maxVal float64) float64

FloatRange returns a random float from minVal to maxVal.

func (*Random) Int

func (r *Random) Int() int

Int returns a random integer.

func (*Random) IntArray

func (r *Random) IntArray(size int, minVal, maxVal int) []int

IntArray returns an array of a given size filled with random int from minVal to maxVal.

func (*Random) IntMax added in v1.0.1

func (r *Random) IntMax(maxVal int) int

IntMax returns a random int from 0 to maxVal.

func (*Random) IntRange

func (r *Random) IntRange(minVal int, maxVal int) int

IntRange returns a random int from minVal to maxVal.

func (*Random) Norm

func (r *Random) Norm(mean, std float64) float64

Norm returns a random number within a normal distribution with the given mean and standard deviation.

func (*Random) NormRange added in v1.0.1

func (r *Random) NormRange(minVal, maxVal, std float64) float64

NormRange returns a random number within a normal distribution (mostly) within a minVal/maxVal range. std is the standard deviation. Something around 0.2 will put almost all the value within the range. Higher values will spread it out more so that a larger percentage of values will be out of the range.

func (*Random) Power

func (r *Random) Power(minVal, maxVal, power float64) float64

Power returns a random number between minVal and maxVal. The higher the power, the more the result will be skewed towards minVal.

func (*Random) RandSeed

func (r *Random) RandSeed()

RandSeed seeds the prng with a random seed.

func (*Random) Seed

func (r *Random) Seed(seed uint64)

Seed sets the prng seed.

func (*Random) Sign

func (r *Random) Sign() float64

Sign returns either -1.0 or 1.0 with equal distribution.

func (*Random) String

func (r *Random) String(length int) string

String returns a random string.

func (*Random) StringAlpha

func (r *Random) StringAlpha(length int) string

StringAlpha returns a random string of letters.

func (*Random) StringFrom

func (r *Random) StringFrom(length int, chars string) string

StringFrom returns a random string of characters from the source string.

func (*Random) StringLower

func (r *Random) StringLower(length int) string

StringLower returns a random lower case string.

func (*Random) StringUpper

func (r *Random) StringUpper(length int) string

StringUpper returns a random upper case string.

func (*Random) WeightedBool

func (r *Random) WeightedBool(weight float64) bool

WeightedBool returns a weighted boolean.

func (Random) WeightedIndex

func (r Random) WeightedIndex(weights []float64) int

WeightedIndex returns a random int based on an array of weights.