Android随机生成字符

时间:2012-08-23 09:09:43

标签: java android

如何生成随机字符?

这样的随机数::

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

我需要画一些像:zCs3v3b1b6只是随机字符

2 个答案:

答案 0 :(得分: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个随机字符的字符串

答案 1 :(得分: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;
}