//随机产生中文
public String generateCN() {
String str = null;
int hightPos, lowPos; // 定义高低位
Random random = new Random();
hightPos = (176 + Math.abs(random.nextInt(39)));// 获取高位值
lowPos = (161 + Math.abs(random.nextInt(93)));// 获取低位值
byte[] b = new byte[2];
b[0] = (new Integer(hightPos).byteValue());
b[1] = (new Integer(lowPos).byteValue());
try {
str = new String(b, "GBk");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}// 转成中文
return str;
}
//随机产生英文
public String generateEN() {
String str = null;
str=(char) (Math.random ()*26+'A')+"";
return str;
}
public String generateCN() {
String str = null;
int hightPos, lowPos; // 定义高低位
Random random = new Random();
hightPos = (176 + Math.abs(random.nextInt(39)));// 获取高位值
lowPos = (161 + Math.abs(random.nextInt(93)));// 获取低位值
byte[] b = new byte[2];
b[0] = (new Integer(hightPos).byteValue());
b[1] = (new Integer(lowPos).byteValue());
try {
str = new String(b, "GBk");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}// 转成中文
return str;
}
//随机产生英文
public String generateEN() {
String str = null;
str=(char) (Math.random ()*26+'A')+"";
return str;
}
本文介绍了一个简单的Java程序,该程序能够随机生成中文字符和英文字符。通过定义高低位值并利用随机数生成原理,实现了中文字符的随机生成;同时,也提供了英文字符的随机生成方法。
3080

被折叠的 条评论
为什么被折叠?



