Skip to content

Random numbers

The math/rand package generates pseudo‑random numbers. For cryptographic randomness, use crypto/rand. Since Go 1.20, the global generator is seeded automatically.

Generate integers, floats, and random permutations.

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)
}
Terminal window
42
0.123456789
1234567890123456789
[2 0 4 1 3]