package com.test;
import java.util.random;
public class generaterandomnumber {
public static void main(string[] args) {
system.out.println("生成的10为随机数为:" + getcharandnumr(10));
}
/**
* java生成随机数字和字母组合
* @param length[生成随机数的长度]
* @return
*/
public static string getcharandnumr(int length) {
string val = "";
random random = new random();
for (int i = 0; i < length; i++) {
// 输出字母还是数字
string charornum = random.nextint(2) % 2 == 0 ? "char" : "num";
// 字符串
if ("char".equalsignorecase(charornum)) {
// 取得大写字母还是小写字母
int choice = random.nextint(2) % 2 == 0 ? 65 : 97;
val += (char) (choice + random.nextint(26));
} else if ("num".equalsignorecase(charornum)) { // 数字
val += string.valueof(random.nextint(10));
}
}
return val;
}
}