获取纯数字或纯字母货数字+字母 /*** 创建时间:2014年12月30日 下午7:40:00* 方法描述:获取纯数字或纯字母货数字+字母* @param number 获取几位的随机数* @param type N是获取number位的纯数字,
/**
* 创建时间:2014年12月30日 下午7:40:00
* 方法描述:获取纯数字或纯字母货数字+字母
* @param number 获取几位的随机数
* @param type N是获取number位的纯数字,c是获取number位的纯小写字母,C是获取number位的纯大写字母,
* NC或CN是获取number位的数字和大写字母组合,Nc和cN获取小写;NCc,cCN,NcC随机获取数字和大小写字母
*/
public static String getRandom(int number, String type) {
String str = "";
Random random = new Random();
if (type.equals("N")) {//数字
for (int i = 0; i < number; i++) {
str += Math.abs(random.nextInt())%9;
}
} else if (type.equals("c")) {//小写字母
for (int i = 0; i < number; i++) {
str += (char)(Math.random()*26 + 97);
}
} else if (type.equals("C")) {//大写字母
for (int i = 0; i < number; i++) {
str += (char)(Math.random()*26 + 65);
}
} else if (type.equals("Nc") || type.equals("cN")) {//数字+小写字母
for (int i = 0; i < number; i++) {
if (Math.abs(random.nextInt())%2 == 0){
str += Math.abs(random.nextInt())%9;
} else {
str += ((char)(Math.random()*26 + 97));
}
}
if (str.matches("\\d*") || str.matches("[a-z]*")) {
getRandom(number, type);
}
} else if (type.equals("NC") || type.equals("CN")) {//数字+大写字母
for (int i = 0; i < number; i++) {
if (Math.abs(random.nextInt())%2 == 0){
str += Math.abs(random.nextInt())%9;
} else {
str += ((char)(Math.random()*26 + 65));
}
}
if (str.matches("\\d*") || str.matches("[A-Z]*")) {
getRandom(number, type);
}
} else if (type.equals("NCc") || type.equals("cCN") || type.equals("NcC")) {//数字+小写字母+大写字母
for (int i = 0; i < number; i++) {
if (Math.abs(random.nextInt())%3 == 0){
str += Math.abs(random.nextInt())%9;
} else if (Math.abs(random.nextInt())%3 == 1) {
str += ((char)(Math.random()*26 + 97));
} else {
str += ((char)(Math.random()*26 + 65));
}
}
if (str.matches("\\d*") || str.matches("[a-z]*") || str.matches("[A-Z]*")) {
getRandom(number, type);
}
}
return str;
}
