MCPcopy Create free account
hub / github.com/Azure/peerd / RandomizedGroups

Function RandomizedGroups

tests/random/math/math.go:38–64  ·  view source on GitHub ↗

RandomizedGroups groups the given collection randomly into groups of size n

(s []string, n int)

Source from the content-addressed store, hash-verified

36
37// RandomizedGroups groups the given collection randomly into groups of size n
38func RandomizedGroups(s []string, n int) [][]string {
39 groups := make([][]string, 0)
40 numGroups := len(s) / n
41 if len(s)%n != 0 {
42 numGroups++
43 }
44
45 // Shuffle the slice
46 for i := range s {
47 j, err := rand.Int(rand.Reader, big.NewInt(int64(i+1)))
48 if err != nil {
49 panic(err)
50 }
51 s[i], s[j.Int64()] = s[j.Int64()], s[i]
52 }
53
54 // Create groups
55 for i := 0; i < numGroups; i++ {
56 group := make([]string, 0)
57 for j := 0; j < n && i*n+j < len(s); j++ {
58 group = append(group, s[i*n+j])
59 }
60 groups = append(groups, group)
61 }
62
63 return groups
64}

Callers 2

benchmarkFunction · 0.92
TestRandomizedGroupsFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestRandomizedGroupsFunction · 0.68