Go: Generate a random number in a given range

Use the rand.Seed and rand.Intn functions in package math/rand to generate a random number between a and b:

rand.Seed(time.Now().UnixNano())
n := a + rand.Intn(b-a+1)

Without the call to rand.Seed, a program will produce the same sequence of pseudo-random numbers for each execution.

Comments