forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
41 lines (33 loc) · 933 Bytes
/
generator.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
package generator
import (
"math/rand"
"strings"
"time"
. "code.cloudfoundry.org/cli/util/words"
)
//go:generate counterfeiter . WordGenerator
type WordGenerator interface {
Babble() string
}
type wordGenerator struct {
numberGenerator *rand.Rand
adjectives []string
nouns []string
}
func (wg wordGenerator) Babble() (word string) {
idx := int(wg.numberGenerator.Int()) % len(wg.adjectives)
word = wg.adjectives[idx] + "-"
idx = int(wg.numberGenerator.Int()) % len(wg.nouns)
word += wg.nouns[idx]
return
}
func NewWordGenerator() WordGenerator {
adjectiveBytes, _ := Asset("util/words/dict/adjectives.txt")
nounBytes, _ := Asset("util/words/dict/nouns.txt")
source := rand.NewSource(time.Now().UnixNano())
return wordGenerator{
adjectives: strings.Split(string(adjectiveBytes), "\n"),
nouns: strings.Split(string(nounBytes), "\n"),
numberGenerator: rand.New(source),
}
}