pkg/random¶
Convenience helpers for building fixtures, tokens, and fuzz data. Functions span deterministic math/rand utilities and crypto-safe password generation.
Quick Start¶
API Highlights¶
Numbers & Booleans¶
Number[T Num](min, max T)– inclusive range for ints/uints,[min,max)for floats. Automatically swaps min/max.Sign()– returns+1or-1.Bool()– coin flip.Choice(slice []T)– pick a random element, panics on empty slice.
Bytes & Strings¶
Byte()– cryptographically secure random byte.String(n)– alphanumeric (upper+lowercase, digits).Alpha(n)/Numeric(n)– letters-only or digits-only.FromCharset(n, charset)– supply your own rune set.
Passwords¶
Options allow mixing letters, digits, symbols, plus excluding ambiguous characters. Generation uses crypto/rand so the output is safe for access tokens and human passwords. Panics if the resulting charset would be empty.
Time Helpers¶
Date(min, max time.Time)– inclusive random timestamp.Duration(min, max time.Duration)– inclusive random duration.
Both helpers swap arguments when min > max, making it easy to mock “recent” or “soon” values without extra branching.
Tips¶
- Only the functions under “Numbers & Booleans” rely on
math/rand; seed it once during startup for non-deterministic sequences. ChoiceandPasswordpanic when misused; wrap them in helper functions if you need error returns instead.- Combine
PasswordOptions{Symbols: true}withtestutils.ContainsAllRunesto assert generated strings hit every required character class in tests.