-
Notifications
You must be signed in to change notification settings - Fork 2
/
rand.go
214 lines (196 loc) · 6.3 KB
/
rand.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
package com
import (
cryptoRand "crypto/rand"
"math"
"math/rand"
"time"
)
// RandomSpec0 Creates a random string based on a variety of options, using
// supplied source of randomness.
//
// If start and end are both 0, start and end are set
// to ' ' and 'z', the ASCII printable
// characters, will be used, unless letters and numbers are both
// false, in which case, start and end are set to 0 and math.MaxInt32.
//
// If set is not nil, characters between start and end are chosen.
//
// This method accepts a user-supplied rand.Rand
// instance to use as a source of randomness. By seeding a single
// rand.Rand instance with a fixed seed and using it for each call,
// the same random sequence of strings can be generated repeatedly
// and predictably.
func randomSpec0(count uint, start, end int, letters, numbers bool,
chars []rune) string {
if count == 0 {
return ""
}
if start == 0 && end == 0 {
end = 'z' + 1
start = ' '
if !letters && !numbers {
start = 0
end = math.MaxInt32
}
}
buffer := make([]rune, count)
gap := end - start
for count != 0 {
count--
var ch rune
if len(chars) == 0 {
ch = rune(rand.Intn(gap) + start)
} else {
ch = chars[rand.Intn(gap)+start]
}
if letters && ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) ||
numbers && (ch >= '0' && ch <= '9') ||
(!letters && !numbers) {
if ch >= rune(56320) && ch <= rune(57343) {
if count == 0 {
count++
} else {
buffer[count] = ch
count--
buffer[count] = rune(55296 + rand.Intn(128))
}
} else if ch >= rune(55296) && ch <= rune(56191) {
if count == 0 {
count++
} else {
// high surrogate, insert low surrogate before putting it in
buffer[count] = rune(56320 + rand.Intn(128))
count--
buffer[count] = ch
}
} else if ch >= rune(56192) && ch <= rune(56319) {
// private high surrogate, no effing clue, so skip it
count++
} else {
buffer[count] = ch
}
} else {
count++
}
}
return string(buffer)
}
// RandomSpec1 Creates a random string whose length is the number of characters specified.
//
// Characters will be chosen from the set of alpha-numeric
// characters as indicated by the arguments.
//
// Param count - the length of random string to create
// Param start - the position in set of chars to start at
// Param end - the position in set of chars to end before
// Param letters - if true, generated string will include
//
// alphabetic characters
//
// Param numbers - if true, generated string will include
//
// numeric characters
func randomSpec1(count uint, start, end int, letters, numbers bool) string {
return randomSpec0(count, start, end, letters, numbers, nil)
}
// RandomAlphaOrNumeric Creates a random string whose length is the number of characters specified.
//
// Characters will be chosen from the set of alpha-numeric
// characters as indicated by the arguments.
//
// Param count - the length of random string to create
// Param letters - if true, generated string will include
//
// alphabetic characters
//
// Param numbers - if true, generated string will include
//
// numeric characters
func randomAlphaOrNumeric(count uint, letters, numbers bool) string {
return randomSpec1(count, 0, 0, letters, numbers)
}
func RandomString(count uint) string {
return randomSpec1(count, 0, 255, false, false)
}
func RandomStringSpec0(count uint, set []rune) string {
return randomSpec0(count, 0, len(set)-1, false, false, set)
}
func RandomStringSpec1(count uint, set string) string {
return RandomStringSpec0(count, []rune(set))
}
// RandomASCII Creates a random string whose length is the number of characters
// specified.
// Characters will be chosen from the set of characters whose
// ASCII value is between 32 and 126 (inclusive).
func RandomASCII(count uint) string {
return randomSpec1(count, 32, 127, false, false)
}
// RandomAlphabetic Creates a random string whose length is the number of characters specified.
// Characters will be chosen from the set of alphabetic characters.
func RandomAlphabetic(count uint) string {
return randomAlphaOrNumeric(count, true, false)
}
// RandomAlphanumeric Creates a random string whose length is the number of characters specified.
// Characters will be chosen from the set of alpha-numeric characters.
func RandomAlphanumeric(count uint) string {
return randomAlphaOrNumeric(count, true, true)
}
// RandomNumeric Creates a random string whose length is the number of characters specified.
// Characters will be chosen from the set of numeric characters.
func RandomNumeric(count uint) string {
return randomAlphaOrNumeric(count, false, true)
}
// RandStr .
func RandStr(count int) (r string) {
//count := 64
b := make([]byte, count)
_, err := cryptoRand.Read(b)
if err != nil {
r = RandomString(uint(count))
} else {
r = string(b)
}
return
}
// RandInt Get in the range [0, max], a random integer type int
func RandInt(max int) int {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return r.Intn(max)
}
// RandFloat32 获取范围为[0.0, 1.0],类型为float32的随机小数
func RandFloat32() float32 {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return r.Float32()
}
// RandFloat64 获取范围为[0.0, 1.0],类型为float64的随机小数
func RandFloat64() float64 {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return r.Float64()
}
// RandPerm 获取范围为[0,max],数量为max,类型为int的随机整数slice
func RandPerm(max int) []int {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return r.Perm(max)
}
// RandRangeInt64 生成区间随机数
// @param int64 min 最小值
// @param int64 max 最大值
// @return int64 生成的随机数
func RandRangeInt64(min, max int64) int64 {
if min >= max || min == 0 || max == 0 {
return max
}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return r.Int63n(max-min) + min
}
// RandRangeInt 生成区间随机数
// @param int min 最小值
// @param int max 最大值
// @return int 生成的随机数
func RandRangeInt(min, max int) int {
if min >= max || min == 0 || max == 0 {
return max
}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return r.Intn(max-min) + min
}