github.com/lingyao2333/mo-zero@v1.4.1/core/stringx/random.go (about) 1 package stringx 2 3 import ( 4 crand "crypto/rand" 5 "fmt" 6 "math/rand" 7 "sync" 8 "time" 9 ) 10 11 const ( 12 letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" 13 letterIdxBits = 6 // 6 bits to represent a letter index 14 idLen = 8 15 defaultRandLen = 8 16 letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits 17 letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits 18 ) 19 20 var src = newLockedSource(time.Now().UnixNano()) 21 22 type lockedSource struct { 23 source rand.Source 24 lock sync.Mutex 25 } 26 27 func newLockedSource(seed int64) *lockedSource { 28 return &lockedSource{ 29 source: rand.NewSource(seed), 30 } 31 } 32 33 func (ls *lockedSource) Int63() int64 { 34 ls.lock.Lock() 35 defer ls.lock.Unlock() 36 return ls.source.Int63() 37 } 38 39 func (ls *lockedSource) Seed(seed int64) { 40 ls.lock.Lock() 41 defer ls.lock.Unlock() 42 ls.source.Seed(seed) 43 } 44 45 // Rand returns a random string. 46 func Rand() string { 47 return Randn(defaultRandLen) 48 } 49 50 // RandId returns a random id string. 51 func RandId() string { 52 b := make([]byte, idLen) 53 _, err := crand.Read(b) 54 if err != nil { 55 return Randn(idLen) 56 } 57 58 return fmt.Sprintf("%x%x%x%x", b[0:2], b[2:4], b[4:6], b[6:8]) 59 } 60 61 // Randn returns a random string with length n. 62 func Randn(n int) string { 63 b := make([]byte, n) 64 // A src.Int63() generates 63 random bits, enough for letterIdxMax characters! 65 for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; { 66 if remain == 0 { 67 cache, remain = src.Int63(), letterIdxMax 68 } 69 if idx := int(cache & letterIdxMask); idx < len(letterBytes) { 70 b[i] = letterBytes[idx] 71 i-- 72 } 73 cache >>= letterIdxBits 74 remain-- 75 } 76 77 return string(b) 78 } 79 80 // Seed sets the seed to seed. 81 func Seed(seed int64) { 82 src.Seed(seed) 83 }