Files
obitools4/pkg/obistats/random.go
Eric Coissac cbd42d5b30 Implements a parallel version of Kmeans
Former-commit-id: 58ab24b9b274451e00eea0275249234e2c2ea09b
2023-08-26 01:26:40 +02:00

36 lines
741 B
Go

package obistats
import "math/rand"
// SampleIntWithoutReplacement generates a random sample of unique integers without replacement.
//
// Generates a random sample of n unique integers without replacement included in the range [0, max).
//
// Parameters:
// - n: the number of integers to generate.
// - max: the maximum value for the generated integers.
//
// Returns:
// - []int: a slice of integers containing the generated sample.
func SampleIntWithoutReplacement(n, max int) []int {
draw := make(map[int]int, n)
for i := 0; i < n; i++ {
y := rand.Intn(max)
x, ok := draw[y]
if ok {
y = x
}
draw[y] = max - 1
max--
}
res := make([]int, 0, n)
for i := range draw {
res = append(res, i)
}
return res
}