Golang built-in package math/rand provides random integer generation in [0,n). With a little modification we can make it works for negative integers too.

func RandInt(lower, upper int) int {
	rand.Seed(time.Now().UnixNano())
	rng := upper - lower
	return rand.Intn(rng) + lower
}

True random number generation is hard. What math/rand does is pseudo-random number generation which is generating a sequence of numbers based on a provided key and it is called seed here. It also means anyone can generate the same sequence of “random” integers if the seed value is known. (For more details, click Here) Even though math/rand is not truely random but we can make it close enough by using a constantly changing key and what we are using here is current Unix time.

Warning:

  • rand.Intn() doesn’t accept anything less than zero so lower and upper should not be the same and upper should always be larger than lower.