-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrandom.go
37 lines (30 loc) · 1017 Bytes
/
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
package gryphon
import (
"math/rand"
"time"
)
// RandomSelectStr returns a string that was randomly selected from a list of strings.
func RandomSelectStr(list []string) string {
rand.Seed(time.Now().UnixNano())
return list[rand.Intn(len(list))]
}
// RandomSelectStrNested returns a string array that was randomly selected from a nested list of strings
func RandomSelectStrNested(list [][]string) []string {
rand.Seed(time.Now().UnixNano())
return list[rand.Intn(len(list))]
}
// RandomSelectInt returns an integer that was randomly selected from a list of integers.
func RandomSelectInt(list []int) int {
rand.Seed(time.Now().UnixNano())
return list[rand.Intn(len(list))]
}
// RandomString randomly generates an alphabetic string of a given length.
func RandomString(n int) string {
rand.Seed(time.Now().UnixNano())
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}