Skip to content

Instantly share code, notes, and snippets.

@hhhaiai
Created July 30, 2020 07:56
Show Gist options
  • Save hhhaiai/6b5729f9633fed44ec9d69253655e786 to your computer and use it in GitHub Desktop.
Save hhhaiai/6b5729f9633fed44ec9d69253655e786 to your computer and use it in GitHub Desktop.
// 生成随机数字和字母
// https://blog.csdn.net/yaodong_y/article/details/8115250
public static String getStringRandomA(int length) {
String val = "";
Random random = null;
// 参数length,表示生成几位随机数
for (int i = 0; i < length; i++) {
random = new Random(System.nanoTime());
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment