github.com/annchain/OG@v0.0.9/common/math/random.go (about)

     1  package math
     2  
     3  import "math/rand"
     4  
     5  // GenerateRandomIndices generates [count] unique random numbers within range [0, upper)
     6  // if count > upper, use all available indices
     7  func GenerateRandomIndices(count int, upper int) []int {
     8  	if count > upper {
     9  		count = upper
    10  	}
    11  	// avoid dup
    12  	generated := make(map[int]struct{})
    13  	for count > len(generated) {
    14  		i := rand.Intn(upper)
    15  		generated[i] = struct{}{}
    16  	}
    17  	arr := make([]int, 0, len(generated))
    18  	for k := range generated {
    19  		arr = append(arr, k)
    20  	}
    21  	return arr
    22  }