-
Notifications
You must be signed in to change notification settings - Fork 5
/
random.go
101 lines (89 loc) · 2.09 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
package utils
import (
rrand "crypto/rand"
"encoding/hex"
"io"
"math"
)
const (
letterBytes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 31 / letterIdxBits // # of letter indices fitting in 31 bits
)
var (
// Rand goroutine-safe, use Rand.xxx instead of rand.xxx
Rand = NewRand()
Seed = FastRand()
)
// RandInt (>=)min - (<)max
func RandInt(min, max int) int {
if max == min {
return min
}
if max < min {
min, max = max, min
}
return FastIntn(max-min) + min
}
// RandUint32 (>=)min - (<)max
func RandUint32(min, max uint32) uint32 {
if max == min {
return min
}
if max < min {
min, max = max, min
}
return FastRandn(max-min) + min
}
// FastIntn this is similar to rand.Intn, but faster.
// A non-negative pseudo-random number in the half-open interval [0,n).
// Return 0 if n <= 0.
func FastIntn(n int) int {
if n <= 0 {
return 0
}
if n <= math.MaxUint32 {
return int(FastRandn(uint32(n)))
}
return int(Rand.Int63n(int64(n)))
}
// RandString a random string, which may contain uppercase letters, lowercase letters and numbers.
// Ref: stackoverflow.icza
func RandString(n int) string {
return B2S(FastRandBytes(n))
}
// RandHex a random string containing only the following characters: 0123456789abcdef
func RandHex(nHalf int) string {
return hex.EncodeToString(FastRandBytes(nHalf))
}
// RandBytes random bytes
func RandBytes(n int) []byte {
if n < 1 {
return nil
}
b := make([]byte, n)
if _, err := io.ReadFull(rrand.Reader, b); err != nil {
return nil
}
return b
}
// FastRandBytes random bytes, but faster.
func FastRandBytes(n int) []byte {
if n < 1 {
return nil
}
b := make([]byte, n)
for i, cache, remain := n-1, FastRand(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = FastRand(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return b
}