mirror of
https://github.com/tim-beatham/smegmesh.git
synced 2024-12-13 18:11:25 +01:00
976dbf2613
certain threshold.
26 lines
527 B
Go
26 lines
527 B
Go
package lib
|
|
|
|
import "math/rand"
|
|
|
|
// RandomSubsetOfLength: Given an array of nodes generate of random
|
|
// subset of 'num' length.
|
|
func RandomSubsetOfLength[V any](vs []V, num int) []V {
|
|
randomSubset := make([]V, 0)
|
|
selectedIndices := make(map[int]struct{})
|
|
|
|
for i := 0; i < num; {
|
|
if len(randomSubset) == len(vs) {
|
|
return randomSubset
|
|
}
|
|
|
|
randomIndex := rand.Intn(len(vs))
|
|
|
|
if _, ok := selectedIndices[randomIndex]; !ok {
|
|
randomSubset = append(randomSubset, vs[randomIndex])
|
|
i++
|
|
}
|
|
}
|
|
|
|
return randomSubset
|
|
}
|