-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
46 lines (39 loc) · 968 Bytes
/
string.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
package workers
import "math/rand"
// Letter will generate a single random lower case ASCII letter
func Letter() string {
return string(randLetter())
}
// Digit will generate a single ASCII digit
func Digit() string {
return string(randDigit())
}
// Lexify will replace ? will random generated letters
func Lexify(str string) string {
return replaceWithLetters(str)
}
// ShuffleStrings will randomize a slice of strings
func ShuffleStrings(a []string) {
swap := func(i, j int) {
a[i], a[j] = a[j], a[i]
}
//to avoid upgrading to 1.10 I copied the algorithm
n := len(a)
if n <= 1 {
return
}
//if size is > int32 probably it will never finish, or ran out of entropy
i := n - 1
for ; i > 0; i-- {
j := int(rand.Int31n(int32(i + 1)))
swap(i, j)
}
}
// RandString will take in a slice of string and return a randomly selected value
func RandString(a []string) string {
size := len(a)
if size == 0 {
return ""
}
return a[rand.Intn(size)]
}