-
Notifications
You must be signed in to change notification settings - Fork 0
/
Randomizer.go
50 lines (41 loc) · 1.19 KB
/
Randomizer.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
package types
import (
"math/rand"
"slices"
"github.com/tibbyrocks/tibby/internal/utils"
)
type Randomizer struct {
items []string
}
// Appends a string to the randomizer slice
func (r *Randomizer) Append(appendableItems ...string) {
r.items = append(r.items, appendableItems...)
}
/*
This function fills the randomizer's items slice with the contents of filepath. filepath points to a file with 1 item per line.
The clear parameter lets you decide whether to clear the slice before filling it (rather than append to it)
*/
func (r *Randomizer) Fill(filepath string, clear bool) {
if clear {
r.clear()
}
content := utils.FileToSlice(filepath)
for _, c := range content {
r.Append(c)
}
}
func (destRand *Randomizer) Combine(srcRand ...*Randomizer) {
for _, appendableRand := range srcRand {
destRand.items = slices.Concat(destRand.items, appendableRand.items)
}
}
// Clears the randomizer slice
func (r *Randomizer) clear() {
clear(r.items)
}
// Produces a random item from the Randomizer slice.
// Examples show the use of rand.Seed but as of Go 1.20 the runtime seeds the generator automatically.
func (r Randomizer) Random() string {
n := rand.Intn(len(r.items))
return r.items[n]
}