Commit 83779fca by guojuxing

添加随机生成N位数的随机数(字母、数字组成)

parent 9d7a9bd5
package com.gic.enterprise.utils;
import java.util.Random;
/**
* 随机生成字母、数字参合的密码
* @ClassName: CreatePasswordAutoUtils

* @Description: 

* @author guojuxing

* @date 2019/8/9 4:39 PM

*/
public class CreatePasswordAutoUtils {
/**
* 随机生成字母、数字参合的密码
* @param length 生成多少位数的密码
* @return
*/
public static String getStringRandom(int length) {
String val = "";
Random random = new Random();
//length为几位密码
for (int i = 0; i < length; i++) {
String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
//输出字母还是数字
if ("char".equalsIgnoreCase(charOrNum)) {
//输出是大写字母还是小写字母
int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
val += (char) (random.nextInt(26) + temp);
} else if ("num".equalsIgnoreCase(charOrNum)) {
val += String.valueOf(random.nextInt(10));
}
}
return val;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment