Java中生成随机字符的方法总结

时间:2023-03-08 16:01:27
package learnExercise;

public class RandomCharacter {
public static char getRandomCharacter(char ch1,char ch2){
return (char)(ch1+Math.random()*(ch2-ch1+1));//因为random<1.0,所以需要+1,才能取到ch2
}
public static char getRandomLowerCaseLetter(){
return getRandomCharacter('a','z');
}
public static char getRandomUpperCaseLetter(){
return getRandomCharacter('A','Z');
}
public static char getRandomDigitLetter(){
return getRandomCharacter('0','9');
} public static char getRandomCharacter(){
return getRandomCharacter('\u0000','\uFFFF');
}
}

上面记录的是生成不同区间随机字符的方法。