Random numbers
Context
Section titled “Context”Go provides random number generation in the math/rand package. For cryptographic randomness, use crypto/rand.
Important functions:
rand.Intn(n)– random integer in[0, n)rand.Int()– random non‑negative integerrand.Float64()– random float in[0.0, 1.0)rand.Seed(seed)– initialize the generator (deprecated in Go 1.20+; now automatic)
Since Go 1.20, the global random generator is seeded automatically at program start.
Example
Section titled “Example”Generate random numbers.
Code example
Section titled “Code example”package main
import ( "fmt" "math/rand" "time")
func main() { // Random integer between 0 and 99 fmt.Println("Random int 0-99:", rand.Intn(100))
// Random float between 0.0 and 1.0 fmt.Println("Random float:", rand.Float64())
// Random integer (non-negative) fmt.Println("Random int:", rand.Int())
// For different sequences each run, we can still seed manually (though not required) rand.NewSource(time.Now().UnixNano()) fmt.Println("Seeded random:", rand.Intn(100))}Output (example, will vary)
Section titled “Output (example, will vary)”Random int 0-99: 42Random float: 0.123456789Random int: 1234567890123456789Seeded random: 87