CodeUtils.java
1.05 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
/**
*
*/
package com.glsx.manager.utils;
/**
* @ClassName: NumberUtils
* @Description: TODO(这里用一句话描述这个类的作用)
* @author Zheng Xiajun
* @date 2019年1月18日
*
*/
public class CodeUtils {
/**
*
* @author Zheng Xiajun
* @Title: generateUniqueRandomCode
* @Description: 生成字母数字混合随机码
* @param digit 需要的随机码位数
* @return String 返回类型
*
*/
public static String generateUniqueRandomCode(int digit) {
String randomcode = "";
// 用字符数组的方式随机
String model = "0123456789abcdefghijklmnopqrstuvwxyz";
char[] m = model.toCharArray();
for (int j = 0; j < digit; j++) {
char c = m[(int) (Math.random() * 36)];
// 保证随机数之间没有重复的
if (randomcode.contains(String.valueOf(c))) {
j--;
continue;
}
randomcode = randomcode + c;
}
return randomcode;
}
public static void main(String args[]){
System.out.println(generateUniqueRandomCode(4));
}
}