Skip to content

Commit

Permalink
修改随机数获取策略
Browse files Browse the repository at this point in the history
  • Loading branch information
CareyWang committed Mar 22, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 58060ab commit e013ce5
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -30,6 +30,11 @@ type redisPoolConf struct {
}

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 defaultExpire = 90
@@ -160,9 +165,20 @@ func longToShort(longUrl string, ttl int) string {
// 产生一个63位随机整数,除以字符数取余获取对应字符
func generate(bits int) string {
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)
}

0 comments on commit e013ce5

Please sign in to comment.