-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.go
225 lines (199 loc) · 6.27 KB
/
random.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package random
import (
"math/rand"
"strings"
"unsafe"
)
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" // 52 characters
const letterDigitBytes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" // 62 characters
const (
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = letterIdxMask / letterIdxBits // # of letter indices fitting in 63 bits
)
// maxBits returns the maximum bits can represent the number, if the number is power of two, plus one.
//
// Like: 0->0, 1->1, 2->2, 3->2, 4->3, 5->3, 8->4
func maxBits(num int) (bits int) {
for ; num >= 1; num >>= 1 {
bits++
}
return bits
}
// RandStringWithLetter only returns a random string(uppercase or lowercase) of length n, with no numbers.
//
// more details ref: https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-go/31832326#31832326
func RandStringWithLetter(n int) string {
return randStringWithLetterDigits(n, false)
}
// RandStringWithLetterDigits only returns a random string(uppercase or lowercase) of length n.
func RandStringWithLetterDigits(n int) string {
return randStringWithLetterDigits(n, true)
}
func randStringWithLetterDigits(n int, containDigits bool) string {
if n <= 0 {
return ""
}
charset := letterBytes
if containDigits {
charset = letterDigitBytes
}
b := make([]byte, n)
// A localRand.Int63() generates 63 random bits, enough for letterIdxMax characters!
for i, cache, remain := n-1, localRand.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = localRand.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(charset) {
b[i] = charset[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return *(*string)(unsafe.Pointer(&b))
}
// RandStringInCharset returns a random string of length n with the given charset.
// One character in the charset maybe use 1byte, 2bytes, 3bytes or 4bytes.
func RandStringInCharset(n int, charset []rune) string {
idxBits := maxBits(len(charset))
idxMask := int64(1<<idxBits - 1)
idxMax := idxMask / int64(idxBits)
b := strings.Builder{}
b.Grow(n)
for i, cache, remain := n-1, localRand.Int63(), idxMax; i >= 0; {
// round repeat
if remain == 0 || cache == 0 {
cache, remain = localRand.Int63(), idxMax
}
if idx := int(cache & idxMask); idx < len(charset) {
b.WriteRune(charset[idx])
i--
}
cache >>= idxBits
remain--
}
return b.String()
}
// RandStringWithKind returns a random string of length n with the given number kind and
// use it`s bits to indicate the inclusion characters kind.
//
// kind=0000, returns contains the characters '0' - '9', 'A' - 'Z' and 'a' - 'z'
// kind=0001, returns only contains the characters '0' - '9'
// kind=0010, returns only contains the characters 'A' - 'Z'
// kind=0100, returns only contains the characters 'a' - 'z'
// kind=1000, the same as kind=0000
// others will be combined the above kinds.
func RandStringWithKind(n int, kind int) []byte {
characters, result := [][]int{{'9' - '0', '0'}, {'Z' - 'A', 'A'}, {'z' - 'a', 'a'}}, make([]byte, n)
var posIndex []int
if kind <= 0 || kind >= 8 {
posIndex = []int{0, 1, 2}
} else {
for i := maxBits(kind); kind > 0; kind &= kind - 1 {
i--
posIndex = append(posIndex, i)
}
}
var ik int
for i := 0; i < n; i++ {
ik = RandIn(posIndex)
count, base := characters[ik][0], characters[ik][1]
result[i] = uint8(base + rand.Intn(count))
}
return result
}
// RandIn returns one random value from the given slice.
// If empty slice will panic.
func RandIn[T any](slice []T) T {
n := len(slice)
if n == 0 {
panic("slice nil")
}
idxBits := maxBits(n)
idxMask := int64(1<<idxBits - 1)
idx := int(localRand.Int63()&idxMask) % n
return slice[idx]
}
// RandNIn returns a specified number of elements random value from the input slice.
func RandNIn[T any](n int, slice []T) []T {
size := len(slice)
if n <= 0 {
return []T{}
} else if n >= size {
n = size
}
m := make([]int, size)
for i := 0; i < size; i++ {
j := localRand.Intn(i + 1)
m[i] = m[j]
m[j] = i
}
ret := make([]T, n)
for i := 0; i < n; i++ {
ret[i] = slice[m[i]]
}
return ret
}
const (
DefaultSALT = 89482311 // SALT
LenLetterDigitBytes = 62
)
// number and nearest prime mapping
var codeLengthNearestPrimeMapping = []uint8{
2, 2, 3, 2, 3, 3, 5, 5,
7, 7, 7, 7, 11, 11, 13, 13,
}
// RandUniCodeByUID returns the random string with the length n.
//
// Warn: the max n shall less than 10.
// n=1, max codes count 62
// n=2, max codes count 62^2=3844
// n=3, max codes count 62^3=238,328
// n=4, max codes count 62^4=14,776,336
// n=5, max codes count 62^5=916,132,832
// n=6, max codes count 62^6=56,800,235,584
// n=7, max codes count 62^7=3,521,614,606,208
// n=8, max codes count 62^8=218,340,105,584,896
// n=9, max codes count 62^9=1.353708655E16
// n=10, max codes count 62^10=8.392993659E17
func RandUniCodeByUID(uid uint64, n int) string {
return RandUniCodeByUIDWithSalt(uid, n, DefaultSALT)
}
// RandUniCodeByUIDWithSalt returns the random string with the length n and your salt.
//
// Warn: the max n shall less than 10.
// n=1, max codes count 62
// n=2, max codes count 62^2=3844
// n=3, max codes count 62^3=238,328
// n=4, max codes count 62^4=14,776,336
// n=5, max codes count 62^5=916,132,832
// n=6, max codes count 62^6=56,800,235,584
// n=7, max codes count 62^7=3,521,614,606,208
// n=8, max codes count 62^8=218,340,105,584,896
// n=9, max codes count 62^9=1.353708655E16
// n=10, max codes count 62^10=8.392993659E17
func RandUniCodeByUIDWithSalt(uid uint64, n int, slat uint64) string {
if n <= 0 {
return ""
} else if n >= 10 {
n = 10
}
// uid*3 = uid<<1 + uid, Co-prime with character set(letterDigitBytes) length 62
uid = uid<<1 + uid + slat
prime := codeLengthNearestPrimeMapping[n]
code := make([]byte, n)
dfIdx := make([]byte, n)
// Diffusion
for i := 0; i < n; i++ {
dfIdx[i] = byte(uid % LenLetterDigitBytes)
dfIdx[i] = (dfIdx[i] + byte(i)*dfIdx[0]) % LenLetterDigitBytes
uid = uid / LenLetterDigitBytes
}
// Confusion
for i := 0; i < n; i++ {
idx := (byte(i) * prime) % byte(n)
code[i] = letterDigitBytes[dfIdx[idx]]
}
return *(*string)(unsafe.Pointer(&code))
}