-
Notifications
You must be signed in to change notification settings - Fork 4
/
random.go
160 lines (131 loc) · 3.06 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
package utils
import (
"crypto/rand"
"encoding/binary"
mathRand "math/rand"
"sync"
_ "unsafe"
)
/*
Creation Time: 2019 - Oct - 03
Created by: (ehsan)
Maintainers:
1. Ehsan N. Moosa (E2)
Auditor: Ehsan N. Moosa (E2)
*/
func init() {
rndGen.New = func() interface{} {
x := mathRand.New(mathRand.NewSource(CPUTicks()))
return x
}
}
const (
digits = "0123456789"
digitsLength = uint32(len(digits))
alphaNumerics = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
alphaNumericsLength = uint32(len(alphaNumerics))
)
// FastRand is a fast thread local random function.
//go:linkname FastRand runtime.fastrand
func FastRand() uint32
type randomGenerator struct {
sync.Pool
}
func (rg *randomGenerator) GetRand() *mathRand.Rand {
return rg.Get().(*mathRand.Rand) //nolint:forcetypeassert
}
func (rg *randomGenerator) PutRand(r *mathRand.Rand) {
rg.Put(r)
}
var rndGen randomGenerator
// RandomID generates a pseudo-random string with length 'n' which characters are alphanumerics.
func RandomID(n int) string {
rnd := rndGen.GetRand()
b := make([]byte, n)
for i := range b {
b[i] = alphaNumerics[FastRand()%alphaNumericsLength]
}
rndGen.PutRand(rnd)
return ByteToStr(b)
}
func RandomIDs(n ...int) []string {
str := make([]string, 0, len(n))
for _, x := range n {
str = append(str, RandomID(x))
}
return str
}
// RandomDigit generates a pseudo-random string with length 'n' which characters are only digits (0-9)
func RandomDigit(n int) string {
rnd := rndGen.GetRand()
b := make([]byte, n)
for i := 0; i < len(b); i++ {
b[i] = digits[FastRand()%digitsLength]
}
rndGen.PutRand(rnd)
return ByteToStr(b)
}
// RandomInt64 produces a pseudo-random 63bit number, if n == 0 there will be no limit otherwise
// the output will be smaller than n
func RandomInt64(n int64) (x int64) {
rnd := rndGen.GetRand()
if n == 0 {
x = rnd.Int63()
} else {
x = rnd.Int63n(n)
}
rndGen.PutRand(rnd)
return
}
// RandomInt32 produces a pseudo-random 31bit number, if n == 0 there will be no limit otherwise
// the output will be smaller than n
func RandomInt32(n int32) (x int32) {
rnd := rndGen.GetRand()
if n == 0 {
x = rnd.Int31()
} else {
x = rnd.Int31n(n)
}
rndGen.PutRand(rnd)
return
}
// SecureRandomInt63 produces a secure pseudo-random 63bit number
func SecureRandomInt63(n int64) (x int64) {
var b [8]byte
_, _ = rand.Read(b[:])
xx := binary.BigEndian.Uint64(b[:])
if n > 0 {
x = int64(xx) % n
} else {
x = int64(xx >> 1)
}
return
}
func RandomInt(n int) (x int) {
rnd := rndGen.GetRand()
if n == 0 {
x = rnd.Int()
} else {
x = rnd.Intn(n)
}
rndGen.PutRand(rnd)
return
}
// RandomUint64 produces a pseudo-random unsigned number
func RandomUint64(n uint64) (x uint64) {
rnd := rndGen.GetRand()
if n == 0 {
x = rnd.Uint64()
} else {
x = rnd.Uint64() % n
}
rndGen.PutRand(rnd)
return
}
// SecureRandomUint64 produces a secure pseudo-random 64bit number
func SecureRandomUint64() (x uint64) {
var b [8]byte
_, _ = rand.Read(b[:])
x = binary.BigEndian.Uint64(b[:])
return
}