RandIntSliceInRange
Returns a slice of n random integers in the specified range.
- Use
make()to create an appropriate slice. - Use
rangeto iterate over the slice,rand.Intn()to generate random numbers between0and the distance betweenminandmax, addingminto them.
import "math/rand"
func RandIntSliceInRange(min, max, n int) []int {
arr := make([]int, n)
for i := range arr {
arr[i] = rand.Intn(max-min) + min
}
return arr
}RandIntSliceInRange(12, 35, 10) // [19 34 29 15 25 21 18 23 32 27]