-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
random.go
109 lines (90 loc) · 2.73 KB
/
random.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"math/rand"
"sort"
"github.com/mroth/weightedrand"
)
// combinations takes input in the form of a map of item lists, and returns a
// list of all combinations of each item for each key. E.g.:
//
// {"foo": [1, 2, 3], "bar": [4, 5, 6]}
//
// Will return the following maps:
//
// {"foo": 1, "bar": 4}
// {"foo": 1, "bar": 5}
// {"foo": 1, "bar": 6}
// {"foo": 2, "bar": 4}
// {"foo": 2, "bar": 5}
// {"foo": 2, "bar": 6}
// {"foo": 3, "bar": 4}
// {"foo": 3, "bar": 5}
// {"foo": 3, "bar": 6}
func combinations(items map[string][]interface{}) []map[string]interface{} {
keys := []string{}
for key := range items {
keys = append(keys, key)
}
sort.Strings(keys)
return combiner(map[string]interface{}{}, keys, items)
}
// combiner is a utility function for combinations.
func combiner(head map[string]interface{}, pending []string, items map[string][]interface{}) []map[string]interface{} {
if len(pending) == 0 {
return []map[string]interface{}{head}
}
key, pending := pending[0], pending[1:]
result := []map[string]interface{}{}
for _, value := range items[key] {
path := map[string]interface{}{}
for k, v := range head {
path[k] = v
}
path[key] = value
result = append(result, combiner(path, pending, items)...)
}
return result
}
// uniformChoice chooses a single random item from the argument list, uniformly weighted.
type uniformChoice []interface{}
func (uc uniformChoice) Choose(r *rand.Rand) interface{} {
return uc[r.Intn(len(uc))]
}
// probSetChoice picks a set of strings based on each string's probability (0-1).
type probSetChoice map[string]float64
func (pc probSetChoice) Choose(r *rand.Rand) []string {
choices := []string{}
for item, prob := range pc {
if r.Float64() <= prob {
choices = append(choices, item)
}
}
return choices
}
// uniformSetChoice picks a set of strings with uniform probability, picking at least one.
type uniformSetChoice []string
func (usc uniformSetChoice) Choose(r *rand.Rand) []string { return usc.ChooseAtLeast(r, 1) }
func (usc uniformSetChoice) ChooseAtLeast(r *rand.Rand, num int) []string {
choices := []string{}
indexes := r.Perm(len(usc))
if num < len(indexes) {
indexes = indexes[:1+randomInRange(r, num, len(indexes)-1)]
}
for _, i := range indexes {
choices = append(choices, usc[i])
}
return choices
}
func randomInRange(r *rand.Rand, min, max int) int { return r.Intn(max-min+1) + min }
type weightedChoice map[string]uint
func (wc weightedChoice) Choose(r *rand.Rand) string {
choices := make([]weightedrand.Choice, 0, len(wc))
for k, v := range wc {
choices = append(choices, weightedrand.NewChoice(k, v))
}
chooser, err := weightedrand.NewChooser(choices...)
if err != nil {
panic(err)
}
return chooser.PickSource(r).(string)
}