1
0
forked from extern/smegmesh

Timer in go that syncs with random nodes in the mesh every

given time interval.
This commit is contained in:
Tim Beatham
2023-10-20 12:41:06 +01:00
parent ec87afc235
commit c200544cee
16 changed files with 208 additions and 30 deletions

25
pkg/lib/random.go Normal file
View File

@ -0,0 +1,25 @@
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(selectedIndices) == len(vs) {
return randomSubset
}
randomIndex := rand.Intn(len(vs))
if _, ok := selectedIndices[randomIndex]; !ok {
randomSubset = append(randomSubset, vs[randomIndex])
i++
}
}
return randomSubset
}