-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.go
48 lines (40 loc) · 1.04 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
package random
import (
"math/rand"
"time"
)
// RandomN 根据最大值生成随机整数
func RandomN(n int) int {
rand.Seed(time.Now().UnixNano())
return rand.Intn(n)
}
// GetRandomBytes 生成随机bytes
func GetRandomBytes(len int) []byte {
s := []byte("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
result := make([]byte, len)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < len; i++ {
result[i] = s[r.Intn(62)]
}
return result
}
// GetCapitalRandom 生成大写随机bytes
func GetCapitalRandom(len int) []byte {
s := []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
result := make([]byte, len)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < len; i++ {
result[i] = s[r.Intn(26)]
}
return result
}
// GetLowerRandom 生成小写随机bytes
func GetLowerRandom(len int) []byte {
s := []byte("abcdefghijklmnopqrstuvwxyz")
result := make([]byte, len)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < len; i++ {
result[i] = s[r.Intn(26)]
}
return result
}