rand.go 394 B

12345678910111213141516171819202122232425
  1. package util
  2. import (
  3. "math/rand"
  4. )
  5. func UniqRands(quantity int, maxval int) []int {
  6. if maxval < quantity {
  7. quantity = maxval
  8. }
  9. intSlice := make([]int, maxval)
  10. for i := 0; i < maxval; i++ {
  11. intSlice[i] = i
  12. }
  13. for i := 0; i < quantity; i++ {
  14. j := rand.Int()%maxval + i
  15. // swap
  16. intSlice[i], intSlice[j] = intSlice[j], intSlice[i]
  17. maxval--
  18. }
  19. return intSlice[0:quantity]
  20. }