Documentation
¶
Overview ¶
Package random provides cryptographically secure random utilities:
- Secure random numbers (crypto/rand): Int, IntRange, Float64, Bytes
- Random strings with custom charsets: String, StringWithCharset
- Sampling & shuffling: Shuffle, Sample, Choice, Permutation
- Random colors: HexColor, RGBColor, HSLColor
- UUID v4 (RFC 4122)
All functions use crypto/rand and are safe for concurrent use.
Index ¶
- Constants
- func Base64URLString(n int) string
- func Bool() bool
- func Bytes(n int) []byte
- func Choice[T any](s []T) T
- func Choices[T any](s []T, k int) []T
- func Float64() float64
- func Float64Range(min, max float64) float64
- func HexColor() string
- func HexString(n int) string
- func Int() int
- func Int64() int64
- func Int64Range(min, max int64) int64
- func Int64n(n int64) int64
- func IntRange(min, max int) int
- func Intn(n int) int
- func LowerString(n int) string
- func MathIntn(n int) int
- func MathShuffle[T any](s []T)
- func NumericString(n int) string
- func Password(n int) string
- func Permutation(n int) []int
- func Read(b []byte) (int, error)
- func Sample[T any](s []T, k int) []T
- func Shuffle(n int, swap func(i, j int))
- func ShuffleBytes(s []byte)
- func ShuffleInts(s []int)
- func String(n int) string
- func StringWithCharset(n int, charset string) string
- func UUID() string
- func UUIDBytes() [16]byte
- func UUIDNoDashes() string
- func UpperString(n int) string
- type HSL
- type RGB
Constants ¶
const ( CharsetAlphaLower = "abcdefghijklmnopqrstuvwxyz" CharsetAlphaUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" CharsetAlpha = CharsetAlphaLower + CharsetAlphaUpper CharsetNumeric = "0123456789" CharsetAlphaNum = CharsetAlpha + CharsetNumeric CharsetHexLower = "0123456789abcdef" CharsetHexUpper = "0123456789ABCDEF" CharsetBase62 = CharsetAlphaNum CharsetBase64URL = CharsetAlphaNum + "-_" CharsetSymbol = "!@#$%^&*()-_=+[]{}|;:,.<>?" )
Predefined character sets for random string generation.
Variables ¶
This section is empty.
Functions ¶
func Base64URLString ¶
Base64URLString returns a random URL-safe base64 string of length n.
func Choice ¶
func Choice[T any](s []T) T
Choice returns a random element from a slice. Panics if the slice is empty.
func Choices ¶
Choices returns k random elements from a slice (with replacement). Panics if the slice is empty or k < 0.
func Float64 ¶
func Float64() float64
Float64 returns a cryptographically secure random float64 in [0.0, 1.0).
func Float64Range ¶
Float64Range returns a cryptographically secure random float64 in [min, max).
func Int64 ¶
func Int64() int64
Int64 returns a cryptographically secure random int64 in [0, 1<<62).
func Int64Range ¶
Int64Range returns a cryptographically secure random int64 in [min, max]. Panics if min > max.
func IntRange ¶
IntRange returns a cryptographically secure random int in [min, max]. Panics if min > max.
func LowerString ¶
LowerString returns a random lowercase-alpha string of length n.
func MathIntn ¶
MathIntn returns a non-cryptographic random int in [0, n) using math/rand/v2. Faster than Intn but not secure.
func MathShuffle ¶
func MathShuffle[T any](s []T)
MathShuffle shuffles a slice using math/rand/v2 (faster, non-crypto). Useful when cryptographic security is not required.
func NumericString ¶
NumericString returns a random numeric string of length n.
func Password ¶
Password returns a random password of length n containing at least one lowercase, one uppercase, one digit, and one symbol. Panics if n < 4.
func Permutation ¶
Permutation returns a random permutation of [0, n) using crypto/rand.
func Read ¶
Read fills b with cryptographically secure random bytes. It is a drop-in replacement for crypto/rand.Read.
func Sample ¶
Sample returns k unique random elements from a slice (without replacement). Panics if k > len(s) or k < 0 or s is empty.
func Shuffle ¶
Shuffle shuffles a slice in-place using a swap function. The swap function is called for indices i and j.
func ShuffleBytes ¶
func ShuffleBytes(s []byte)
ShuffleBytes shuffles a byte slice in-place using crypto/rand.
func ShuffleInts ¶
func ShuffleInts(s []int)
ShuffleInts shuffles an int slice in-place using crypto/rand.
func StringWithCharset ¶
StringWithCharset returns a random string of length n using the given charset. Panics if charset is empty or n < 0.
func UUID ¶
func UUID() string
UUID returns a random UUID v4 string in canonical form (e.g. "550e8400-e29b-41d4-a716-446655440000").
func UUIDBytes ¶
func UUIDBytes() [16]byte
UUIDBytes returns 16 random bytes formatted as a UUID v4. The returned bytes have the version and variant bits set.
func UUIDNoDashes ¶
func UUIDNoDashes() string
UUIDNoDashes returns a random UUID v4 string without dashes (32 hex characters).
func UpperString ¶
UpperString returns a random uppercase-alpha string of length n.
Types ¶
type HSL ¶
type HSL struct{ H, S, L float64 }
HSL represents an HSL color with H in [0, 360), S/L in [0, 1].