The question asks how to generate random numbers between min and max where max is not inclusive meaning max will never be reached.
For example, if min is 1 and max is 4 then it would only give you values from 1-3.
If you came here looking how to generate all numbers in min and max range then you need to adjust for that by adding +1.
rand.Intn(max + 1 - min) + min
Example of it working by showing which numbers were hit and how many times:
package main import ( "fmt" "math/rand" ) func main() { dat := make(map[int]int) for i := 0; i < 100000; i++ { x := getRandomInt(1, 4) dat[x] += 1 } fmt.Println(dat) } func getRandomInt(min, max int) int { return rand.Intn(max + 1 - min) + min }
Output:
map[1:24810 2:25194 3:25024 4:24972]
You can also randomly generate negative numbers. If we made min -5 and max 5 we get:
Output:
map[-5:9302 -4:9121 -3:9136 -2:8951 -1:9116 0:8928 1:9156 2:8970 3:9188 4:9045 5:9087]
[min,max]. Well, maybe a matter of taste...mix of positive and negative numberswill be unbalanced.