-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRandomFun.java
More file actions
184 lines (159 loc) · 4.24 KB
/
Copy pathRandomFun.java
File metadata and controls
184 lines (159 loc) · 4.24 KB
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
package io.github.xfuns.java.fun;
import io.github.xfuns.java.Fun;
import java.util.concurrent.ThreadLocalRandom;
/**
* RandomFun
*
* @author smallmenu
*/
public class RandomFun {
/**
* 随机大写字符串池
*/
public static final String RANDOM_UPPER_LETTER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/**
* 随机小写字符串池
*/
public static final String RANDOM_LOWER_LETTER = "abcdefghijklmnopqrstuvwxyz";
/**
* 随机数字池
*/
public static final String RANDOM_NUMBER = "0123456789";
/**
* 随机大小写字符串池
*/
public static final String RANDOM_LETTER = RANDOM_UPPER_LETTER + RANDOM_LOWER_LETTER;
/**
* 随机字符和数字池
*/
public static final String RANDOM_LETTER_NUMBER = RANDOM_LETTER + RANDOM_NUMBER;
/**
* 禁止实例化
*/
private RandomFun() {
throw new AssertionError();
}
/**
* ThreadLocalRandom 对象
*
* @return ThreadLocalRandom
*/
public static ThreadLocalRandom getRandom() {
return ThreadLocalRandom.current();
}
/**
* 获得随机数
*
* @return int
*/
public static int randomInt() {
return getRandom().nextInt();
}
/**
* 获得指定范围内的随机数
*
* @param min 最小数(包含)
* @param max 最大数(不包含)
* @return int
*/
public static int randomInt(int min, int max) {
return getRandom().nextInt(min, max);
}
/**
* 获得指定范围内的随机数 [0,max)
*
* @param max 限制随机数的范围,不包含这个数
* @return int
*/
public static int randomInt(int max) {
return getRandom().nextInt(max);
}
/**
* 获得指定范围内的随机数[min, max)
*
* @param min 最小数(包含)
* @param max 最大数(不包含)
* @return long
*/
public static long randomLong(long min, long max) {
return getRandom().nextLong(min, max);
}
/**
* 获得随机数
*
* @return long
*/
public static long randomLong() {
return getRandom().nextLong();
}
/**
* 获得指定范围内的随机数 [0, max)
*
* @param max 限制随机数的范围,不包括这个数
* @return long
*/
public static long randomLong(long max) {
return getRandom().nextLong(max);
}
/**
* 获得一个随机的字符串(只包含数字和字符)
*
* @param length 字符串的长度
* @return String
*/
public static String randomString(int length) {
return randomPool(RANDOM_LETTER_NUMBER, length);
}
/**
* 获得一个随机的字符串,排除指定的字符串集
*
* @param length 字符串的长度
* @param excepts 排除的字符串列表
* @return String
*/
public static String randomStringExcept(int length, String... excepts) {
String pool = RANDOM_LETTER_NUMBER;
pool = Fun.removeAny(pool, excepts);
return randomPool(pool, length);
}
/**
* 获得一个只包含数字的字符串
*
* @param length 字符串的长度
* @return String
*/
public static String randomNumber(int length) {
return randomPool(RANDOM_NUMBER, length);
}
/**
* 获得一个只包含字符的字符串
*
* @param length 字符串的长度
* @return String
*/
public static String randomLetter(int length) {
return randomPool(RANDOM_LETTER, length);
}
/**
* 获得一个随机的字符串
*
* @param baseString 随机字符选取的样本集
* @param length 字符串的长度
* @return String
*/
public static String randomPool(String baseString, int length) {
if (Fun.empty(baseString)) {
return StringFun.EMPTY;
}
final StringBuilder sb = new StringBuilder(length);
if (length < 1) {
length = 1;
}
int baseLength = baseString.length();
for (int i = 0; i < length; i++) {
int number = randomInt(baseLength);
sb.append(baseString.charAt(number));
}
return sb.toString();
}
}