math/rand
Mathimport "math/rand"Implements pseudo-random number generators for various distributions.
Example
package main
import (
"fmt"
"math/rand"
)
func main() {
fmt.Println(rand.Intn(100)) // 0-99
fmt.Println(rand.Float64()) // 0.0-1.0
}Key Types & Functions
IntnFloat64SeedShuffleNewSourceAbout math/rand
The math/rand package (imported as math/rand) belongs to the Math category of Go packages. Implements pseudo-random number generators for various distributions.
Go's standard library is one of the language's greatest strengths, providing production-ready implementations for networking, cryptography, encoding, I/O, and more. The math/rand package follows Go's philosophy of simplicity and composability — small, focused packages that combine through interfaces like io.Reader and io.Writer.
When using math/rand in production, follow Go best practices: handle errors explicitly, use context for cancellation and timeouts, prefer composition over inheritance, and write table-driven tests. The Go documentation at pkg.go.dev provides comprehensive API references and examples for every exported type and function.