math/rand Package
The math/rand package in Go lets you generate random numbers. Since Go 1.20, the global random number generator is automatically seeded, so you can use it without any initialization.
Syntax
import "math/rand"
// Returns a random integer in [0, n).
n := rand.Intn(100)
// Returns a random float64 in [0.0, 1.0).
f := rand.Float64()
// Go 1.20+: Create a local random number generator.
r := rand.New(rand.NewSource(42)) // Fixed with seed 42.
n2 := r.Intn(10)
// Shuffle a slice randomly.
s := []int{1, 2, 3, 4, 5}
rand.Shuffle(len(s), func(i, j int) { s[i], s[j] = s[j], s[i] })
Function List
| Function | Description |
|---|---|
| rand.Intn(n) | Returns a random int in [0, n). |
| rand.Int() | Returns a random non-negative int. |
| rand.Int63() | Returns a random non-negative int64. |
| rand.Float64() | Returns a random float64 in [0.0, 1.0). |
| rand.Float32() | Returns a random float32 in [0.0, 1.0). |
| rand.Shuffle(n, swap) | Randomly reorders n elements using the swap function. |
| rand.New(src) | Creates a random number generator that uses the specified Source. |
| rand.NewSource(seed) | Creates a Source with the specified seed value (reproducible). |
Sample Code
package main
import (
"fmt"
"math/rand"
)
func main() {
// --- Global random number generator (auto-seeded since Go 1.20) ---
fmt.Println("--- Random integers ---")
for i := 0; i < 5; i++ {
fmt.Printf("rand.Intn(100) = %d\n", rand.Intn(100))
}
fmt.Println()
// Random floating-point numbers.
fmt.Println("--- Random float64 ---")
for i := 0; i < 3; i++ {
fmt.Printf("rand.Float64() = %.4f\n", rand.Float64())
}
fmt.Println()
// Random integers in a custom range (e.g., a die roll from 1 to 6).
fmt.Println("--- Dice roll (1–6) ---")
for i := 0; i < 5; i++ {
dice := rand.Intn(6) + 1 // Intn(6) returns 0–5, so add 1.
fmt.Printf("Dice: %d\n", dice)
}
fmt.Println()
// Shuffling a slice.
fmt.Println("--- Shuffle ---")
s := []int{1, 2, 3, 4, 5}
fmt.Println("Before shuffle:", s)
rand.Shuffle(len(s), func(i, j int) {
s[i], s[j] = s[j], s[i]
})
fmt.Println("After shuffle:", s)
fmt.Println()
// Generate reproducible random numbers with a fixed seed.
fmt.Println("--- Fixed seed (reproducible) ---")
r1 := rand.New(rand.NewSource(42))
r2 := rand.New(rand.NewSource(42))
fmt.Println("r1 numbers:", r1.Intn(100), r1.Intn(100), r1.Intn(100))
fmt.Println("r2 numbers:", r2.Intn(100), r2.Intn(100), r2.Intn(100))
// Same seed produces the same results.
}
Notes
Since Go 1.20, the global random number generator is automatically seeded randomly on each run. This means the old initialization code rand.Seed(time.Now().UnixNano()) is no longer needed. If you need reproducible results in tests, create a local generator with rand.New(rand.NewSource(seed)).
The random numbers generated by math/rand are not cryptographically secure. For security-sensitive values such as passwords or tokens, use the crypto/rand package instead.
For general math functions, see 'math package'.
If you find any errors or copyright issues, please contact us.