mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 00:22:41 +08:00
随机
This commit is contained in:
131
rand/rand.go
Normal file
131
rand/rand.go
Normal file
@ -0,0 +1,131 @@
|
|||||||
|
package rand
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"io"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
mrnd "math/rand"
|
||||||
|
|
||||||
|
"github.com/charlienet/go-mixed/bytesconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
lowercase = "abcdefghijklmnopqrstuvwxyz"
|
||||||
|
digit = "0123456789"
|
||||||
|
nomix = "BCDFGHJKMPQRTVWXY2346789"
|
||||||
|
letter = uppercase + lowercase
|
||||||
|
allChars = uppercase + lowercase + digit
|
||||||
|
hex = digit + "ABCDEF"
|
||||||
|
_ = allChars + "/+"
|
||||||
|
)
|
||||||
|
|
||||||
|
type charScope struct {
|
||||||
|
bytes []byte
|
||||||
|
length int
|
||||||
|
max int
|
||||||
|
bits int
|
||||||
|
mask int
|
||||||
|
lenFunc func(int) int
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
|
||||||
|
for scope.mask < len {
|
||||||
|
scope.bits++
|
||||||
|
scope.mask = 1<<scope.bits - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
scope.max = scope.mask / scope.bits
|
||||||
|
|
||||||
|
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) // 所有字符
|
||||||
|
)
|
||||||
|
|
||||||
|
var randSource mrnd.Source = mrnd.NewSource(time.Now().UnixNano())
|
||||||
|
var randLock sync.Mutex
|
||||||
|
|
||||||
|
// 生成指定长度的随机字符串
|
||||||
|
func (scope *charScope) RandString(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; {
|
||||||
|
if remain == 0 {
|
||||||
|
cache, remain = randInt63(), scope.max
|
||||||
|
}
|
||||||
|
|
||||||
|
if idx := int(cache & int64(scope.mask)); idx < scope.length {
|
||||||
|
ret[i] = scope.bytes[idx]
|
||||||
|
i--
|
||||||
|
}
|
||||||
|
|
||||||
|
cache >>= int64(scope.bits)
|
||||||
|
remain--
|
||||||
|
}
|
||||||
|
|
||||||
|
return bytesconv.BytesToString(ret)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取指定范围内的随机数
|
||||||
|
func RandInt(min, max int) int {
|
||||||
|
n := randNumber2(max - min)
|
||||||
|
return n + min
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成指定范围的随机数
|
||||||
|
func RandInt32(min, max int32) int32 {
|
||||||
|
return int32(RandInt(int(min), int(max)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func RandBytes(len int) ([]byte, error) {
|
||||||
|
r := make([]byte, len)
|
||||||
|
_, err := io.ReadFull(rand.Reader, r)
|
||||||
|
return r, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// func randNumber(max int64) (int64, error) {
|
||||||
|
// r, err := rand.Int(rand.Reader, big.NewInt(max+1))
|
||||||
|
// return r.Int64(), err
|
||||||
|
// }
|
||||||
|
|
||||||
|
func randInt63() int64 {
|
||||||
|
var v int64
|
||||||
|
|
||||||
|
randLock.Lock()
|
||||||
|
v = randSource.Int63()
|
||||||
|
randLock.Unlock()
|
||||||
|
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
func randNumber2(max int) int {
|
||||||
|
rnd := mrnd.New(randSource)
|
||||||
|
return rnd.Intn(max)
|
||||||
|
}
|
118
rand/rand_test.go
Normal file
118
rand/rand_test.go
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
package rand_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
mrnd "math/rand"
|
||||||
|
|
||||||
|
"github.com/charlienet/go-mixed/rand"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRandString(t *testing.T) {
|
||||||
|
t.Log(rand.AllChars.RandString(20))
|
||||||
|
|
||||||
|
// b, err := rand.RandBytes(32)
|
||||||
|
// t.Log(err)
|
||||||
|
// t.Log(hex.EncodeToString(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRandHex(t *testing.T) {
|
||||||
|
h := rand.Hex.RandString(8)
|
||||||
|
t.Log(h)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRandMax(t *testing.T) {
|
||||||
|
mrnd.Seed(time.Now().UnixNano())
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkParallel(b *testing.B) {
|
||||||
|
rand.Hex.RandString(16)
|
||||||
|
|
||||||
|
b.RunParallel(func(pb *testing.PB) {
|
||||||
|
for pb.Next() {
|
||||||
|
rand.Hex.RandString(16)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkNoop(b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
rand.AllChars.RandString(16)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkRandString(b *testing.B) {
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
rand.Hex.RandString(10)
|
||||||
|
}
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
rand.Hex.RandString(20)
|
||||||
|
}
|
||||||
|
|
||||||
|
// b.Run("randString", func(b *testing.B) {
|
||||||
|
|
||||||
|
// for i := 0; i < b.N; i++ {
|
||||||
|
// rand.Hex.RandString(256)
|
||||||
|
// }
|
||||||
|
// })
|
||||||
|
|
||||||
|
// for i := 0; i < b.N; i++ {
|
||||||
|
// rand.RandBytes(16)
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkString(b *testing.B) {
|
||||||
|
elems := []byte("abcdefghijk")
|
||||||
|
|
||||||
|
b.Run("1", func(b *testing.B) {
|
||||||
|
a := []byte{}
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
for _, elem := range elems {
|
||||||
|
a = append(a, elem)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
b.Run("2", func(b *testing.B) {
|
||||||
|
a := make([]byte, len(elems))
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
for _, elem := range elems {
|
||||||
|
a = append(a, elem)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
b.Run("3", func(b *testing.B) {
|
||||||
|
a := make([]byte, len(elems))
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
a = append(a, elems...)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkConcatString(b *testing.B) {
|
||||||
|
elems := []string{"1", "2", "3", "4", "5"}
|
||||||
|
|
||||||
|
b.Run("add", func(b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
ret := ""
|
||||||
|
for _, elem := range elems {
|
||||||
|
ret += elem
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
b.Run("buffer", func(b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
for _, elem := range elems {
|
||||||
|
buf.WriteString(elem)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
Reference in New Issue
Block a user