engine/utils/random.go

50 lines
1.1 KiB
Go
Raw Normal View History

2024-02-11 01:31:01 +08:00
package utils
import (
2024-02-21 20:33:47 +08:00
"math/rand/v2"
2024-02-11 01:31:01 +08:00
)
2024-02-21 20:33:47 +08:00
// 随机生成字符串
2024-02-11 01:31:01 +08:00
func RandomString(l int) string {
str := "0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
bytes := []byte(str)
var result []byte = make([]byte, 0, l)
for i := 0; i < l; i++ {
2024-02-21 20:58:31 +08:00
result = append(result, bytes[rand.IntN(len(bytes))])
2024-02-11 01:31:01 +08:00
}
2024-02-21 20:58:31 +08:00
return string(result)
2024-02-11 01:31:01 +08:00
}
2024-02-21 20:33:47 +08:00
// 随机生成纯字符串
2024-02-11 01:31:01 +08:00
func RandomPureString(l int) string {
str := "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
bytes := []byte(str)
var result []byte = make([]byte, 0, l)
for i := 0; i < l; i++ {
2024-02-21 20:58:31 +08:00
result = append(result, bytes[rand.IntN(len(bytes))])
2024-02-11 01:31:01 +08:00
}
2024-02-21 20:58:31 +08:00
return string(result)
}
// 随机生成纯大写字符串
func RandomPureUpString(l int) string {
str := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
bytes := []byte(str)
var result []byte = make([]byte, 0, l)
for i := 0; i < l; i++ {
result = append(result, bytes[rand.IntN(len(bytes))])
}
return string(result)
2024-02-11 01:31:01 +08:00
}
2024-02-21 20:33:47 +08:00
// 随机生成数字字符串
2024-02-11 01:31:01 +08:00
func RandomNumber(l int) string {
str := "0123456789"
bytes := []byte(str)
var result []byte
for i := 0; i < l; i++ {
2024-02-21 20:58:31 +08:00
result = append(result, bytes[rand.IntN(len(bytes))])
2024-02-11 01:31:01 +08:00
}
2024-02-21 20:58:31 +08:00
return string(result)
2024-02-11 01:31:01 +08:00
}