golang random code example
Example 1: go random number
import (
"math/rand"
)
rand.Intn(10)
Example 2: go rand seed
rand.Seed(time.Now().UTC().UnixNano())
Example 3: select a random number between 1 and 5 in golang
package main
import (
"fmt"
"math/rand"
"time" #ADDED
)
func main() {
rand.Seed(time.Now().UTC().UnixNano()) #ADDED
fmt.Println(randInt(1, 1000))
}
func randInt(min int, max int) int {
return min + rand.Intn(max-min)
}
Example 4: random number golang
x = rand.Intn(max - min) + min
x = rand.Float64()
x = (rand.Float64() * (max - min)) + min