math/rand — deterministic unless seededimport (
"math/rand"
"time"
)
rand.Seed(time.Now().UnixNano()) // legacy global seed; prefer NewSource in new code
r := rand.New(rand.NewSource(42))
n := r.Intn(100)
For crypto-safe randomness (tokens, keys), use crypto/rand.
crypto/randimport "crypto/rand"
b := make([]byte, 16)
_, err := rand.Read(b)
Read bytes from crypto/rand, then hex/base64 encode. Or use a small library for UUID layout.
math/rand for securityPredictable sequences break auth and session IDs.