生成随机汉字的几个方法
方法1: 根据Unicode生成
汉字Unicode编码的区间为:0x4E00 → 0x9FA5
char result = (char) (0x4e00 + (int) (Math.random() * (0x9fa5 - 0x4e00 + 1)));
System.out.println("result : " + result);
方法2: 根据GBK编码生成
String str = "";
int highPos;
int lowPos;
Random random = new Random();
highPos = (176 + Math.abs(random.nextInt(39)));
lowPos = (161 + Math.abs(random.nextInt(93)));
byte[] b = new byte[2];
b[0] = (Integer.valueOf(highPos)).byteValue();
b[1] = (Integer.valueOf(lowPos)).byteValue();
try {
str = new String(b, "GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
char result = str.charAt(0);
System.out.println("result : " + result);