RandomizedGroups groups the given collection randomly into groups of size n
(s []string, n int)
| 36 | |
| 37 | // RandomizedGroups groups the given collection randomly into groups of size n |
| 38 | func 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 | } |
no outgoing calls