Generating random numbers over a range in Go

前端 未结 5 2010
花落未央
花落未央 2021-01-07 16:27

All the integer functions in math/rand generate non-negative numbers.

rand.Int() int              // [0, MaxInt]
rand.Int31() int32          // [0, MaxInt32]         


        
5条回答
  •  失恋的感觉
    2021-01-07 17:11

    I found this example at Go Cookbook, which is equivalent to rand.Range(min, max int) (if that function existed):

    rand.Intn(max - min) + min
    

提交回复
热议问题