Is there functionality to generate a random character in Java?

前端 未结 18 2085
难免孤独
难免孤独 2020-11-28 06:00

Does Java have any functionality to generate random characters or strings? Or must one simply pick a random integer and convert that integer\'s ascii code to a character?

18条回答
  •  隐瞒了意图╮
    2020-11-28 06:20

    In following 97 ascii value of small "a".

    public static char randomSeriesForThreeCharacter() {
    Random r = new Random();
    char random_3_Char = (char) (97 + r.nextInt(3));
    return random_3_Char;
    }
    

    in above 3 number for a , b , c or d and if u want all character like a to z then you replace 3 number to 25.

提交回复
热议问题