修改随机数获取策略

This commit is contained in:
CareyWong 2020-03-22 18:02:29 +08:00
parent 58060ab20a
commit e013ce5889

20
main.go
View File

@ -30,6 +30,11 @@ type redisPoolConf struct {
} }
const letterBytes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" const letterBytes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const (
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
const defaultPort int = 8002 const defaultPort int = 8002
const defaultExpire = 90 const defaultExpire = 90
@ -160,9 +165,20 @@ func longToShort(longUrl string, ttl int) string {
// 产生一个63位随机整数除以字符数取余获取对应字符 // 产生一个63位随机整数除以字符数取余获取对应字符
func generate(bits int) string { func generate(bits int) string {
b := make([]byte, bits) b := make([]byte, bits)
for i := range b {
b[i] = letterBytes[rand.Int63()%int64(len(letterBytes))] // A rand.Int63() generates 63 random bits, enough for letterIdxMax letters!
for i, cache, remain := bits-1, rand.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = rand.Int63(), letterIdxMax
} }
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b) return string(b)
} }