rand seed
This commit is contained in:
parent
8c6c1fa8c3
commit
b2324e60a8
8
main.go
8
main.go
@ -164,13 +164,15 @@ func longToShort(longUrl string, ttl int) string {
|
|||||||
return shortKey
|
return shortKey
|
||||||
}
|
}
|
||||||
|
|
||||||
// 产生一个63位随机整数,除以字符数取余获取对应字符
|
// 产生一个63位随机整数
|
||||||
func generate(bits int) string {
|
func generate(bits int) string {
|
||||||
b := make([]byte, bits)
|
b := make([]byte, bits)
|
||||||
|
|
||||||
currentTime := time.Now().Unix()
|
currentTime := time.Now().UnixNano()
|
||||||
|
rand.Seed(currentTime)
|
||||||
|
|
||||||
for i := range b {
|
for i := range b {
|
||||||
b[i] = letterBytes[(currentTime + rand.Int63()) % int64(len(letterBytes))]
|
b[i] = letterBytes[rand.Intn(len(letterBytes))]
|
||||||
}
|
}
|
||||||
return string(b)
|
return string(b)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user