mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-19 09:02:39 +08:00
随机数生成优化
This commit is contained in:
79
rand/rand.go
79
rand/rand.go
@ -3,11 +3,8 @@ package rand
|
||||
import (
|
||||
"crypto/rand"
|
||||
"io"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"math/big"
|
||||
mrnd "math/rand"
|
||||
|
||||
"github.com/charlienet/go-mixed/bytesconv"
|
||||
)
|
||||
@ -23,36 +20,33 @@ const (
|
||||
_ = allChars + "/+"
|
||||
)
|
||||
|
||||
var (
|
||||
randSource mrnd.Source = mrnd.NewSource(time.Now().UnixNano())
|
||||
randLock sync.Mutex
|
||||
)
|
||||
|
||||
func init() {
|
||||
mrnd.Seed(time.Now().UnixNano())
|
||||
}
|
||||
var rng = NewRandGenerator()
|
||||
|
||||
type charScope struct {
|
||||
bytes []byte
|
||||
length int
|
||||
max int
|
||||
bits int
|
||||
mask int
|
||||
lenFunc func(int) int
|
||||
bytes []byte
|
||||
length int
|
||||
max int
|
||||
bits int
|
||||
mask int
|
||||
}
|
||||
|
||||
var (
|
||||
Uppercase = StringScope(uppercase) // 大写字母
|
||||
Lowercase = StringScope(lowercase) // 小写字母
|
||||
Digit = StringScope(digit) // 数字
|
||||
Nomix = StringScope(nomix) // 不混淆字符
|
||||
Letter = StringScope(letter) // 字母
|
||||
Hex = StringScope(hex) // 十六进制字符
|
||||
AllChars = StringScope(allChars) // 所有字符
|
||||
)
|
||||
|
||||
func StringScope(str string) *charScope {
|
||||
return strScope(str, nil)
|
||||
}
|
||||
|
||||
func strScope(str string, f func(int) int) *charScope {
|
||||
len := len(str)
|
||||
|
||||
scope := &charScope{
|
||||
bytes: bytesconv.StringToBytes(str),
|
||||
length: len,
|
||||
lenFunc: f,
|
||||
bits: 1,
|
||||
bytes: bytesconv.StringToBytes(str),
|
||||
length: len,
|
||||
bits: 1,
|
||||
}
|
||||
|
||||
for scope.mask < len {
|
||||
@ -65,27 +59,14 @@ func strScope(str string, f func(int) int) *charScope {
|
||||
return scope
|
||||
}
|
||||
|
||||
var (
|
||||
Uppercase = StringScope(uppercase) // 大写字母
|
||||
Lowercase = StringScope(lowercase) // 小写字母
|
||||
Digit = StringScope(digit) // 数字
|
||||
Nomix = StringScope(nomix) // 不混淆字符
|
||||
Letter = StringScope(letter) // 字母
|
||||
Hex = strScope(hex, func(n int) int { return n * 2 }) // 十六进制字符
|
||||
AllChars = StringScope(allChars) // 所有字符
|
||||
)
|
||||
|
||||
// 生成指定长度的随机字符串
|
||||
func (scope *charScope) Generate(length int) string {
|
||||
n := length
|
||||
if scope.lenFunc != nil {
|
||||
n = scope.lenFunc(n)
|
||||
}
|
||||
|
||||
ret := make([]byte, n)
|
||||
for i, cache, remain := n-1, randInt63(), scope.max; i >= 0; {
|
||||
|
||||
for i, cache, remain := n-1, rng.Int63(), scope.max; i >= 0; {
|
||||
if remain == 0 {
|
||||
cache, remain = randInt63(), scope.max
|
||||
cache, remain = rng.Int63(), scope.max
|
||||
}
|
||||
|
||||
if idx := int(cache & int64(scope.mask)); idx < scope.length {
|
||||
@ -106,7 +87,7 @@ type scopeConstraint interface {
|
||||
|
||||
// 生成区间 n >= 0, n < max
|
||||
func Intn[T scopeConstraint](max T) T {
|
||||
n := mrnd.Int63n(int64(max))
|
||||
n := rng.Int63n(int64(max))
|
||||
return T(n) % max
|
||||
}
|
||||
|
||||
@ -133,17 +114,3 @@ func RandBytes(len int) ([]byte, error) {
|
||||
_, err := io.ReadFull(rand.Reader, r)
|
||||
return r, err
|
||||
}
|
||||
|
||||
func randInt63() int64 {
|
||||
var v int64
|
||||
|
||||
randLock.Lock()
|
||||
v = randSource.Int63()
|
||||
randLock.Unlock()
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
func randNumber2(max int) int {
|
||||
return mrnd.Intn(max)
|
||||
}
|
||||
|
Reference in New Issue
Block a user