Generating random numbers over a range in Go

前端 未结 5 2032
花落未央
花落未央 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条回答
  •  -上瘾入骨i
    2021-01-07 17:17

    Solution that worked for me is: j = rand.Intn(600) - 100 where m is 100 and n is 500, it will generate numbers from -100 to 499.

提交回复
热议问题