random

package
v0.0.0-...-fa376ca Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AlphaNumeric

func AlphaNumeric(n int) string

AlphaNumeric generates a random string of n alphanumeric characters using the default random source.

func Bool

func Bool() bool

Bool returns a random boolean value.

func Byte

func Byte() byte

Byte returns a random byte using the default random source.

func BytesN

func BytesN(n int, low, high byte) []byte

BytesN generates a random slice of n bytes in the range [low, high] using the default random source.

func BytesSeqN

func BytesSeqN(n int, low, high byte) string

BytesSeqN generates a random slice of n bytes as a string.

func DigitsN

func DigitsN(n int) string

DigitsN generates a random string of n digits using the default random source.

func ExpFloat64

func ExpFloat64() float64

ExpFloat64 returns a random float64 with an exponential distribution.

func Float32

func Float32() float32

Float32 returns a random float32 in the range [0.0, 1.0).

func Float32Range

func Float32Range(min, max float32) float32

Float32Range returns a random float32 in the range [min, max).

func Float64

func Float64() float64

Float64 returns a random float64 in the range [0.0, 1.0).

func Float64Range

func Float64Range(min, max float64) float64

Float64Range returns a random float64 in the range [min, max).

func Int

func Int() int

Int returns a non-negative random int.

func Int32

func Int32() int32

Int32 returns a random 32-bit integer as an int32.

func Int32N

func Int32N(max int32) int32

Int32N returns a random 32-bit integer in the range [0, max).

func Int32Range

func Int32Range(min, max int32) int32

Int32Range returns a random 32-bit integer in the range [min, max).

func Int64

func Int64() int64

Int64 returns a random 64-bit integer as an int64.

func Int64N

func Int64N(max int64) int64

Int64N returns a random 64-bit integer in the range [0, max).

func Int64Range

func Int64Range(min, max int64) int64

Int64Range returns a random 64-bit integer in the range [min, max).

func IntN

func IntN(max int) int

IntN returns a random integer in the range [0, max).

func IntRange

func IntRange(min, max int) int

IntRange returns a random integer in the range [min, max).

func Letters

func Letters(n int) string

Letters generates a random string of n letters (mixed case) using the default random source.

func LowerN

func LowerN(n int) string

LowerN generates a random string of n lowercase letters using the default random source.

func NormFloat64

func NormFloat64() float64

NormFloat64 returns a random float64 with a normal distribution.

func Perm

func Perm(n int) []int

Perm returns a slice of n distinct random non-negative integers in [0, n)

func Rune

func Rune() rune

Rune returns a random rune using the default random source.

func RunesN

func RunesN(n int, low, high rune) []rune

RunesN generates a random slice of n runes in the range [low, high] using the default random source.

func RunesSeq

func RunesSeq(n int, low, high rune) string

RunesSeq generates a random slice of n runes as a string.

func SecAlphaNumeric

func SecAlphaNumeric(n int) string

SecAlphaNumeric generates a cryptographically secure random string of n alphanumeric characters.

func SecBool

func SecBool() bool

SecBool returns a secure random boolean value.

func SecByte

func SecByte() byte

SecByte returns a cryptographically secure random byte.

func SecBytesN

func SecBytesN(n int, low, high byte) []byte

SecBytesN generates a cryptographically secure random slice of n bytes in the range [low, high].

func SecBytesSeqN

func SecBytesSeqN(n int, low, high byte) string

SecBytesSeqN generates a cryptographically secure random slice of n bytes as a string.

func SecDigitsN

func SecDigitsN(n int) string

SecDigitsN generates a cryptographically secure random string of n digits.

func SecExpFloat64

func SecExpFloat64() float64

SecExpFloat64 returns a secure random float64 with an exponential distribution.

func SecFloat32

func SecFloat32() float32

SecFloat32 returns a secure random float32 in the range [0.0, 1.0).

func SecFloat32Range

func SecFloat32Range(min, max float32) float32

SecFloat32Range returns a secure random float32 in the range [min, max).

func SecFloat64

func SecFloat64() float64

SecFloat64 returns a secure random float64 in the range [0.0, 1.0).

func SecFloat64Range

func SecFloat64Range(min, max float64) float64

SecFloat64Range returns a secure random float64 in the range [min, max).

func SecInt

func SecInt() int

SecInt returns a secure random non-negative int.

func SecInt32

func SecInt32() int32

SecInt32 returns a secure random 32-bit integer.

func SecInt32N

func SecInt32N(n int32) int32

SecInt32N returns a secure random 32-bit integer in the range [0, n).

func SecInt32Range

func SecInt32Range(min, max int32) int32

SecInt32Range returns a secure random 32-bit integer in the range [min, max).

func SecInt64

func SecInt64() int64

SecInt64 returns a secure random 64-bit integer.

func SecInt64N

func SecInt64N(n int64) int64

SecInt64N returns a secure random 64-bit integer in the range [0, n).

func SecInt64Range

func SecInt64Range(min, max int64) int64

SecInt64Range returns a secure random 64-bit integer in the range [min, max).

func SecIntN

func SecIntN(n int) int

SecIntN returns a secure random integer in the range [0, n).

func SecIntRange

func SecIntRange(min, max int) int

SecIntRange returns a secure random integer in the range [min, max).

func SecLetters

func SecLetters(n int) string

SecLetters generates a cryptographically secure random string of n letters (mixed case).

func SecLowerN

func SecLowerN(n int) string

SecLowerN generates a cryptographically secure random string of n lowercase letters.

func SecNormFloat64

func SecNormFloat64() float64

SecNormFloat64 returns a secure random float64 with a normal distribution.

func SecPerm

func SecPerm(n int) []int

SecPerm returns a slice of n distinct random non-negative integers in [0, n) using a cryptographically secure random number generator.

func SecRune

func SecRune() rune

SecRune returns a cryptographically secure random rune.

func SecRunesN

func SecRunesN(n int, low, high rune) []rune

SecRunesN generates a cryptographically secure random slice of n runes in the range [low, high].

func SecRunesSeq

func SecRunesSeq(n int, low, high rune) string

SecRunesSeq generates a cryptographically secure random slice of n runes as a string.

func SecShuffle

func SecShuffle(n int, swap func(i, j int))

SecShuffle shuffles the first n elements of data using a cryptographically secure random number generator.

func SecTime

func SecTime(since, until time.Time) time.Time

SecTime returns a random time within the given range [since, until) using the secure source.

func SecUint

func SecUint() uint

SecUint returns a secure random unsigned int.

func SecUint32

func SecUint32() uint32

SecUint32 returns a secure random 32-bit unsigned integer.

func SecUint32N

func SecUint32N(n uint32) uint32

SecUint32N returns a secure random 32-bit unsigned integer in the range [0, n).

func SecUint32Range

func SecUint32Range(min, max uint32) uint32

SecUint32Range returns a secure random 32-bit unsigned integer in the range [min, max).

func SecUint64

func SecUint64() uint64

SecUint64 returns a secure random 64-bit unsigned integer.

func SecUint64N

func SecUint64N(max uint64) uint64

SecUint64N returns a secure random 64-bit unsigned integer in the range [0, n).

func SecUint64Range

func SecUint64Range(min, max uint64) uint64

SecUint64Range returns a secure random 64-bit unsigned integer in the range [min, max).

func SecUintN

func SecUintN(n uint) uint

SecUintN returns a secure random unsigned integer in the range [0, n).

func SecUintRange

func SecUintRange(min, max uint) uint

SecUintRange returns a secure random unsigned integer in the range [min, max).

func SecUpperN

func SecUpperN(n int) string

SecUpperN generates a cryptographically secure random string of n uppercase letters.

func Shuffle

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

Shuffle shuffles the first n elements of data using the default source.

func Time

func Time(since, until time.Time) time.Time

Time returns a random time within the given range [since, until) using the default source.

func Uint

func Uint() uint

Uint returns a random unsigned int.

func Uint32

func Uint32() uint32

Uint32 returns a random 32-bit unsigned integer as a uint32.

func Uint32N

func Uint32N(max uint32) uint32

Uint32N returns a random 32-bit unsigned integer in the range [0, max).

func Uint32Range

func Uint32Range(min, max uint32) uint32

Uint32Range returns a random 32-bit unsigned integer in the range [min, max).

func Uint64

func Uint64() uint64

Uint64 returns a random 64-bit unsigned integer as a uint64.

func Uint64N

func Uint64N(max uint64) uint64

Uint64N returns a random 64-bit unsigned integer in the range [0, max).

func Uint64Range

func Uint64Range(min, max uint64) uint64

Uint64Range returns a random 64-bit unsigned integer in the range [min, max).

func UintN

func UintN(max uint) uint

UintN returns a random unsigned integer in the range [0, max).

func UintRange

func UintRange(min, max uint) uint

UintRange returns a random unsigned integer in the range [min, max).

func Upper

func Upper(n int) string

Upper generates a random string of n uppercase letters using the default random source.

Types

type Rand

type Rand struct {
	*rand.Rand
}

Rand is an enhance number generator for *math/rand/v2.Rand. #nosec G404 (CWE-338): Use of weak random number generator

func New

func New(source Source) *Rand

New returns a new Rand using the provided source. #nosec G404 (CWE-338): Use of weak random number generator

func NewChaCha8

func NewChaCha8() *Rand

NewChaCha8 returns a new non-concurrent-safe ChaCha8 random number generator. ChaCha8Rng is a variant of the ChaCha stream cipher, designed for high-speed encryption and random number generation. It offers: - Strong cryptographic properties - Excellent performance on a wide range of platforms - Resistance to timing attacks - Simple and efficient implementation This implementation uses PCG to generate the initial seed. #nosec G404 (CWE-338): Use of weak random number generator

func NewCrypto

func NewCrypto() *Rand

NewCrypto returns a new concurrent-safe cryptographically secure random number generator. It utilizes the operating system's cryptographically secure random number generator: - Windows: Uses CryptGenRandom, which leverages the CryptoAPI - Linux: Uses getrandom() system call, falling back to /dev/urandom if unavailable - macOS: Uses SecRandomCopyBytes, which is backed by the Yarrow algorithm This generator offers: - High-quality, unpredictable random numbers suitable for cryptographic use - Protection against common cryptographic attacks - Automatic seeding from system entropy sources - Consistent interface across different operating systems Note: While highly secure, this generator may be slower than non-cryptographic alternatives.

func NewCryptoBackoff

func NewCryptoBackoff(backoff *Rand) *Rand

NewCryptoBackoff returns a new concurrent-safe cryptographically secure random number generator with a specified backoff generator.

func NewPCG

func NewPCG() *Rand

NewPCG returns a new non-concurrent-safe PCG random number generator. PCG (Permuted Congruential Generator) is a family of simple, fast, and statistically excellent random number generators. It offers: - High statistical quality output - Small state size and fast period advancement - Excellent performance on various platforms - Multiple, independent streams - Simple and efficient implementation This implementation uses runtime and system information to generate the initial seeds. #nosec G404 (CWE-338): Use of weak random number generator

func NewRuntime

func NewRuntime() *Rand

NewRuntime returns a new concurrent-safe, high-speed random number generator. This generator offers: - Concurrent safety through goroutine-local random number generators - High performance suitable for non-cryptographic use cases - Based on the ChaCha8 algorithm for quality randomness - Automatic seeding from runtime information Concurrency safety is achieved by utilizing Go's runtime, where each goroutine has its own local random number generator. This approach eliminates the need for locks or atomic operations, resulting in excellent performance in concurrent scenarios. The underlying algorithm is ChaCha8, which provides a good balance of speed and randomness quality. Note: While fast and suitable for many applications, this is not cryptographically secure.

func NewZipf

func NewZipf() *Rand

NewZipf returns a new non-concurrent-safe Zipf-distributed random number generator. Zipf distribution is a discrete probability distribution commonly used for modeling: - Word frequencies in natural languages - Population sizes of cities - Income distribution in economics It offers: - Power-law behavior for modeling real-world phenomena - Adjustable parameters for different distribution shapes - Efficient generation of skewed random numbers - Suitable for various applications in data science and simulations This implementation uses PCG as the underlying random source with dynamic parameters. #nosec G404 (CWE-338): Use of weak random number generator

func (*Rand) AlphaNumericN

func (r *Rand) AlphaNumericN(n int) string

AlphaNumericN generates a random string of n alphanumeric characters.

func (*Rand) Bool

func (r *Rand) Bool() bool

Bool returns a random boolean value.

func (*Rand) Byte

func (r *Rand) Byte() byte

Byte returns a random byte.

func (*Rand) BytesN

func (r *Rand) BytesN(n int, low, high byte) []byte

BytesN generates a random slice of n bytes in the range [low, high].

func (*Rand) DigitsN

func (r *Rand) DigitsN(n int) string

DigitsN generates a random string of n digits.

func (*Rand) Float32Range

func (r *Rand) Float32Range(min, max float32) float32

Float32Range returns a random float32 in the range [min, max).

func (*Rand) Float64Range

func (r *Rand) Float64Range(min, max float64) float64

Float64Range returns a random float64 in the range [min, max).

func (*Rand) Int32Range

func (r *Rand) Int32Range(min, max int32) int32

Int32Range returns a random int32 in the range [min, max).

func (*Rand) Int64Range

func (r *Rand) Int64Range(min, max int64) int64

Int64Range returns a random int64 in the range [min, max).

func (*Rand) IntRange

func (r *Rand) IntRange(min, max int) int

IntRange returns a random int in the range [min, max).

func (*Rand) LettersN

func (r *Rand) LettersN(n int) string

LettersN generates a random string of n letters (mixed case).

func (*Rand) LowerN

func (r *Rand) LowerN(n int) string

LowerN generates a random string of n lowercase letters.

func (*Rand) Rune

func (r *Rand) Rune() rune

Rune returns a random rune.

func (*Rand) RunesN

func (r *Rand) RunesN(n int, low, high rune) []rune

RunesN generates a random slice of n runes in the range [low, high].

func (*Rand) Time

func (r *Rand) Time(since, until time.Time) time.Time

Time returns a random time within the given range [since, until).

func (*Rand) Uint32Range

func (r *Rand) Uint32Range(min, max uint32) uint32

Uint32Range returns a random uint32 in the range [min, max).

func (*Rand) Uint64Range

func (r *Rand) Uint64Range(min, max uint64) uint64

Uint64Range returns a random uint64 in the range [min, max).

func (*Rand) UintRange

func (r *Rand) UintRange(min, max uint) uint

UintRange returns a random uint in the range [min, max).

func (*Rand) UpperN

func (r *Rand) UpperN(n int) string

UpperN generates a random string of n uppercase letters.

type Source

type Source interface {
	rand.Source
}

Source is an interface for generating random numbers. #nosec G404 (CWE-338): Use of weak random number generator

Jump to

Keyboard shortcuts

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