Java 随机数问题

来源:百度知道 编辑:UC知道 时间:2024/06/14 21:05:34
混合产生6位字母与数字的随机数,应该怎样生成?一次生成若干组,要求各不相同。例如10组。
字母不区分大小写

public class Tester {

private static final String[] CHARS = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};

public static String randomString(int length) {
char c[] = new char[length];
for (int i = 0; i < length; i++) {
int r = (int) (Math.random() * CHARS.length);
c[i] = r % 2 == 0 ? CHARS[r].toUpperCase().charAt(0) : CHARS[r].charAt(0);
}
return new String(c);
}

public static void main(String[] args) {