0

如何生成随机字符?

像这样的随机数::

public static int random() {
    Random generator = new Random();
    int x = generator.nextInt(10000);
    return x;
}

我需要画出类似的东西:zCs3v3b1b6 just random chars

4

2 回答 2

2
public static char random() {
     Random generator = new Random();
     char x = (char)(generator.nextInt(96) + 32);
     return x;
}

这将生成一个随机字符。

public static void main(String[] args) {
     StringBuilder builder = new StringBuilder();
     for (int i = 0; i < 8; i++)
           builder.append(random());
     System.out.println(builder.toString());
}

这将生成一个包含 8 个随机字符的字符串

于 2012-08-23T09:13:31.433 回答
0

见以下链接:http ://www.asciitable.com/

public static char randomSeriesForThreeCharacter() {
Random r = new Random();
char random_3_Char = (char) (48 + r.nextInt(47));
return random_3_Char;
}
于 2013-01-30T07:22:38.030 回答