-
Notifications
You must be signed in to change notification settings - Fork 0
/
rand_string.go
39 lines (31 loc) · 1.1 KB
/
rand_string.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package tg
import (
"math/rand"
"time"
)
// letterBytes defines the characters used for generating random strings.
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
// letterIdxBits represents the number of bits required to represent a letter index.
const letterIdxBits = 6
// letterIdxMask is a bitmask used to extract the letter index from the random number.
const letterIdxMask = 1<<letterIdxBits - 1
// letterIdxMax is the maximum number of letter indices that can fit in 63 bits.
const letterIdxMax = 63 / letterIdxBits
// RandStringBytes generates a random string of the specified length.
func RandStringBytes(n int) string {
src := rand.NewSource(time.Now().UnixNano())
b := make([]byte, n)
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b)
}