Random numbers
Context
Section titled “Context”The math/rand package generates pseudo‑random numbers. For cryptographic randomness, use crypto/rand. Since Go 1.20, the global generator is seeded automatically.
Example
Section titled “Example”Generate integers, floats, and random permutations.
Code example
Section titled “Code example”package main
import ( "fmt" "math/rand")
func main() { fmt.Println(rand.Intn(100)) // 0-99 fmt.Println(rand.Float64()) // 0.0-1.0 fmt.Println(rand.Int()) // non‑negative
// Permutation of [0..n) perm := rand.Perm(5) fmt.Println(perm)}Output (example)
Section titled “Output (example)”420.1234567891234567890123456789[2 0 4 1 3]