74 lines
1.7 KiB
Go
74 lines
1.7 KiB
Go
package utils
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/rand"
|
|
"fmt"
|
|
"math/big"
|
|
mr "math/rand"
|
|
"time"
|
|
)
|
|
|
|
type random struct{}
|
|
|
|
func Random() *random {
|
|
return &random{}
|
|
}
|
|
|
|
// GetRandomInt 获取指定长度的随机数字
|
|
func (r random) GetRandomInt(len int) string {
|
|
var numbers = []byte{0, 1, 2, 3, 4, 5, 7, 8, 9}
|
|
var container string
|
|
length := bytes.NewReader(numbers).Len()
|
|
|
|
for i := 1; i <= len; i++ {
|
|
rd, err := rand.Int(rand.Reader, big.NewInt(int64(length)))
|
|
if err != nil {
|
|
|
|
}
|
|
container += fmt.Sprintf("%d", numbers[rd.Int64()])
|
|
}
|
|
return container
|
|
}
|
|
|
|
// GetRandomNumber 获取指定范围内的一个随机数
|
|
func (r random) GetRandomNumber(min, max int) int {
|
|
mr.Seed(time.Now().UnixNano())
|
|
rn := mr.Intn(max-min) + min
|
|
return rn
|
|
}
|
|
|
|
// GetLiveHouseId 获取腾讯TRTC直播间Id
|
|
func (r random) GetLiveHouseId() string {
|
|
id := r.GetRandomNumber(1, 4294967295)
|
|
return fmt.Sprintf("%d", id)
|
|
}
|
|
|
|
// GetRandomString 生成随机字符串
|
|
func (r random) GetRandomString(len int) string {
|
|
var container string
|
|
var str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
|
|
b := bytes.NewBufferString(str)
|
|
length := b.Len()
|
|
bigInt := big.NewInt(int64(length))
|
|
for i := 0; i < len; i++ {
|
|
randomInt, _ := rand.Int(rand.Reader, bigInt)
|
|
container += string(str[randomInt.Int64()])
|
|
}
|
|
return container
|
|
}
|
|
|
|
// GetRandomStringSafe 获取去掉了iI0O1的随机字符串
|
|
func (r random) GetRandomStringSafe(len int) string {
|
|
var container string
|
|
var str = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789"
|
|
b := bytes.NewBufferString(str)
|
|
length := b.Len()
|
|
bigInt := big.NewInt(int64(length))
|
|
for i := 0; i < len; i++ {
|
|
randomInt, _ := rand.Int(rand.Reader, bigInt)
|
|
container += string(str[randomInt.Int64()])
|
|
}
|
|
return container
|
|
}
|