Golang Generate Random Number

Kevin FOO
Mar 3, 2022

--

Golang random number generator needs a seed in order to really generate a random number. Without seed, if you ran the randomizer 10 times now and 10 times a minute later, you will notice that the numbers produced are the same.

package mainimport (
"fmt"
"time"
"math/rand"
)
func main() {
for z:=0; z<9; z++ {
rand.Seed(time.Now().UTC().UnixNano())
fmt.Println("My favorite number is", rand.Intn(10))
}
}

< Back to all the stories I had written

--

--

Kevin FOO
Kevin FOO

Written by Kevin FOO

A software engineer, a rock climbing, inline skating enthusiast, a husband, a father.