-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.go
58 lines (54 loc) · 1.75 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
49
50
51
52
53
54
55
56
57
58
package util
import (
crand "crypto/rand"
"encoding/base64"
"math"
"math/rand"
)
func RandomBase64(lenStr int) (string, error) {
// 计算至少需要的原始字节数量,向上取整
minBytesNeeded := int(math.Ceil(float64(lenStr*3) / 4))
// 生成原始字节
b := make([]byte, minBytesNeeded)
if _, err := crand.Read(b); err != nil {
return "", err
}
// 进行Base64编码
base64Str := base64.URLEncoding.EncodeToString(b)
// 注意这可能会有'='填充字符 截断处理
return base64Str[:lenStr], nil
}
func RandomID(lenStr int) string {
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
const (
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits of letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
// 计算至少需要的原始字节数量,向上取整
b := make([]byte, lenStr)
for i, cache, remain := lenStr-1, rand.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = rand.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letters) {
b[i] = letters[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b)[:lenStr]
}
func RandomCode(length int) string {
// 创建一个长度为length的切片,用于存放生成的随机数
randomNumbers := make([]byte, length)
// 填充切片,生成指定长度的随机数
// rand.Intn(base) 返回一个[0, base)之间的伪随机整数
// 这里base是10,因为我们只需要0-9的数字
for i := range randomNumbers {
randomNumbers[i] = byte(rand.Intn(10)) + '0' // 将整数转换为对应的字符'0'-'9'
}
// 将切片转换为字符串
return string(randomNumbers)
}