Returns a random integer in the specified range.
Use rand.Intn()
to generate a random number between 0
and the distance between min
and max
and add min
to it.
代码实现
import "math/rand"
func RandIntInRange(min, max int) int {
return rand.Intn(max-min) + min
}
使用样例
RandIntInRange(0, 5) // 2