-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwords.go
65 lines (51 loc) · 950 Bytes
/
words.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
package randomizer
// word options
const (
adjective int = iota
noun
)
// Noun returns a random noun
func Noun() string {
return randomize(jsonContent.Nouns...)
}
// Adjective returns a random adjective
func Adjective() string {
return randomize(jsonContent.Adjectives...)
}
// Word returns a random word, either an adjective or a noun
func Word() string {
const options = 2 // # of enum options
s := getRandSource()
randLock.Lock()
n := s.Intn(options)
randLock.Unlock()
switch n {
case adjective:
return Adjective()
default:
return Noun()
}
}
// Words returns a slice of words of count n
func Words(n int) []string {
var words = make([]string, n)
i := 0
for i < n {
words[i] = Word()
i++
}
return words
}
func randomize(l ...string) string {
if len(l) == 0 {
return ""
}
if len(l) == 1 {
return l[0]
}
s := getRandSource()
randLock.Lock()
n := s.Intn(len(l) - 1)
randLock.Unlock()
return l[n]
}