文章目录
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
public class RandomUtils {
// 制定字符串范围的因子
public static final String NUMBERS_AND_LETTERS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static final String NUMBERS = "0123456789";
public static final String LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static final String CAPITAL_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static final String LOWER_CASE_LETTERS = "abcdefghijklmnopqrstuvwxyz";
// 获取随机数字、字母字符串
public static String getRandomNumbersAndLetters(int length) {
return getRandom(NUMBERS_AND_LETTERS, length);
}
// 获取随机数字字符串
public static String getRandomNumbers(int length) {
return getRandom(NUMBERS, length);
}
// 获取英文字母组成的随机字符串
public static String getRandomLetters(int length) {
return getRandom(LETTERS, length);
}
// 获取大写字母组成的随机字符串
public static String getRandomCapitalLetters(int length) {
return getRandom(CAPITAL_LETTERS, length);
}
// 获取小写字母组成的随机字符串
public static String getRandomLowerCaseLetters(int length) {
return getRandom(LOWER_CASE_LETTERS, length);
}
// 获取制定字符串和长度的随机字符串
public static String getRandom(String source, int length) {
return TextUtils.isEmpty(source) ? null : getRandom(
source.toCharArray(), length);
}
// 获取制定字符数组和长度的随机字符串
public static String getRandom(char[] sourceChar, int length) {
if (sourceChar == null || sourceChar.length == 0 || length < 0) {
return null;
}

StringBuilder str = new StringBuilder(length);
Random random = new Random();
for (int i = 0; i < length; i++) {
str.append(sourceChar[random.nextInt(sourceChar.length)]);
}
return str.toString();
}

// 获取0~max范围的随机int
public static int getRandom(int max) {
return getRandom(0, max);
}

// 获取min~max范围的随机int
public static int getRandom(int min, int max) {
if (min > max) {
return 0;
}
if (min == max) {
return min;
}
return min + new Random().nextInt(max - min);
}

}
文章目录