Answers for "golang random number in range"

Go
0

select a random number between 1 and 5 in golang

package main
import (
    "fmt"
    "math/rand"
    "time"    #ADDED
)

func main() {
    // Seed should be set once, better spot is func init()
    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)
}
Posted by: Guest on June-22-2020
1

golang random number in range

rand.Intn(max - min) + min
Posted by: Guest on March-23-2020

Browse Popular Code Answers by Language