forked from chenssy89/jutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomUtils.java
More file actions
96 lines (88 loc) · 2.57 KB
/
RandomUtils.java
File metadata and controls
96 lines (88 loc) · 2.57 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
package com.JUtils.math;
import java.util.Random;
/**
* 随机数工具类
*
* @Author:chenssy
* @date:2014年8月11日
*/
public class RandomUtils {
private static final String ALL_CHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final String LETTER_CHAR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final String NUMBER_CHAR = "0123456789";
/**
* 获取定长的随机数,包含大小写、数字
* @autor:chenssy
* @date:2014年8月11日
*
* @param length
* 随机数长度
* @return
*/
public static String generateString(int length) {
StringBuffer sb = new StringBuffer();
Random random = new Random();
for (int i = 0; i < length; i++) {
sb.append(ALL_CHAR.charAt(random.nextInt(ALL_CHAR.length())));
}
return sb.toString();
}
/**
* 获取定长的随机数,包含大小写字母
* @autor:chenssy
* @date:2014年8月11日
*
* @param length
* 随机数长度
* @return
*/
public static String generateMixString(int length) {
StringBuffer sb = new StringBuffer();
Random random = new Random();
for (int i = 0; i < length; i++) {
sb.append(LETTER_CHAR.charAt(random.nextInt(LETTER_CHAR.length())));
}
return sb.toString();
}
/**
* 获取定长的随机数,只包含小写字母
* @autor:chenssy
* @date:2014年8月11日
*
* @param length
* 随机数长度
* @return
*/
public static String generateLowerString(int length) {
return generateMixString(length).toLowerCase();
}
/**
* 获取定长的随机数,只包含大写字母
* @autor:chenssy
* @date:2014年8月11日
*
* @param length
* 随机数长度
* @return
*/
public static String generateUpperString(int length) {
return generateMixString(length).toUpperCase();
}
/**
* 获取定长的随机数,只包含数字
* @autor:chenssy
* @date:2014年8月11日
*
* @param length
* 随机数长度
* @return
*/
public static String generateNumberString(int length){
StringBuffer sb = new StringBuffer();
Random random = new Random();
for (int i = 0; i < length; i++) {
sb.append(NUMBER_CHAR.charAt(random.nextInt(NUMBER_CHAR.length())));
}
return sb.toString();
}
}