java中产生随机汉字

hyb2008dxy 2008-05-07 02:05:25
java中如何产生一个随机的汉字,不用预存在数组的方式来做的话,有没有其他方法?
...全文
1427 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
kuaihuoxian 2009-05-07
  • 打赏
  • 举报
回复
7楼的好
wyhlgx 2008-06-13
  • 打赏
  • 举报
回复
不行,这些汉字很多不认识。有的是繁体的。
jpinglove 2008-05-08
  • 打赏
  • 举报
回复
晕,都是牛人,这些代码,我也收藏了!
hq19880508 2008-05-08
  • 打赏
  • 举报
回复
是不是无论在C或者JAVA中定义一个随机函数之前都要先设置好种子seed??
landyshouguo 2008-05-07
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 javazhuobin 的回复:]
给LZ写了一个,不晓得要得不?
import java.util.Date;
import java.util.Random;

public class Chinese {
public String getChinese(long seed) throws Exception {
String str = null;
int highPos, lowPos;
seed = new Date().getTime();
Random random = new Random(seed);
highPos = (176 + Math.abs(random.nextInt(39)));
lowPos = 161 + Math.abs(random.nextInt(93));
byte[] b = new byte[2];

[/Quote]顶
老紫竹 2008-05-07
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 bao110908 的回复:]
Java codepublic class Test1 {
public static void main(String[] args) {
RandomHan han = new RandomHan();
System.out.println(han.getRandomHan());
}
}

class RandomHan {
private Random ran = new Random();
private final static int delta = 0x9fa5 - 0x4e00 + 1;

public char getRandomHan() {
return (char)(0x4e00 + ran.nextInt(delta));
}
}
[/Quote]

顶一个
FL1429 2008-05-07
  • 打赏
  • 举报
回复
标记下....
chenhongxin 2008-05-07
  • 打赏
  • 举报
回复
Random random = new Random(seed);
highPos = (176 + Math.abs(random.nextInt(39)));
lowPos = 161 + Math.abs(random.nextInt(93));
byte[] b = new byte[2];
b[0] = (new Integer(highPos)).byteValue();
b[1] = (new Integer(lowPos)).byteValue();
str = new String(b, "GB2312");
return str;
}
chenhongxin 2008-05-07
  • 打赏
  • 举报
回复
import java.util.Date;
import java.util.Random;

public class Chinese {
public String getChinese(long seed) throws Exception {
String str = null;
int highPos, lowPos;
seed = new Date().getTime();
Random random = new Random(seed);
highPos = (176 + Math.abs(random.nextInt(39)));
lowPos = 161 + Math.abs(random.nextInt(93));
byte[] b = new byte[2];
b[0] = (new Integer(highPos)).byteValue();
b[1] = (new Integer(lowPos)).byteValue();
str = new String(b, "GB2312");
return str;
}

public static String get300Chinese() throws Exception {
Chinese ch = new Chinese();
String str = "";
for (int i = 300; i > 0; i--) {
str = str + ch.getChinese(i);

}
System.out.println(str);
return str;
}

public static void main(String[] args) throws Exception {

get300Chinese();
}
}
qusic 2008-05-07
  • 打赏
  • 举报
回复
mark·
  • 打赏
  • 举报
回复
public class Test1 {
public static void main(String[] args) {
RandomHan han = new RandomHan();
System.out.println(han.getRandomHan());
}
}

class RandomHan {
private Random ran = new Random();
private final static int delta = 0x9fa5 - 0x4e00 + 1;

public char getRandomHan() {
return (char)(0x4e00 + ran.nextInt(delta));
}
}
刘彬彬 2008-05-07
  • 打赏
  • 举报
回复
给LZ写了一个,不晓得要得不?
import java.util.Date;
import java.util.Random;

public class Chinese {
public String getChinese(long seed) throws Exception {
String str = null;
int highPos, lowPos;
seed = new Date().getTime();
Random random = new Random(seed);
highPos = (176 + Math.abs(random.nextInt(39)));
lowPos = 161 + Math.abs(random.nextInt(93));
byte[] b = new byte[2];
b[0] = (new Integer(highPos)).byteValue();
b[1] = (new Integer(lowPos)).byteValue();
str = new String(b, "GB2312");
return str;
}

public static String get300Chinese() throws Exception {
Chinese ch = new Chinese();
String str = "";
for (int i = 300; i > 0; i--) {
str = str + ch.getChinese(i);

}
System.out.println(str);
return str;
}

public static void main(String[] args) throws Exception {

get300Chinese();
}
}
allenblade 2008-05-07
  • 打赏
  • 举报
回复
晕,4位16进制随机数阿。。。范围就是上面得
云上飞翔 2008-05-07
  • 打赏
  • 举报
回复
[Quote=引用楼主 hyb2008dxy 的帖子:]
java中如何产生一个随机的汉字,不用预存在数组的方式来做的话,有没有其他方法?
[/Quote]
答:参考代码如下:

//在0x4e00---0x9fa5之间产生一个随机的字符
public static char getRandomChar()
{
return (char)(0x4e00+(int)(Math.random()*(0x9fa5-0x4e00+1)));
}
hyb2008dxy 2008-05-07
  • 打赏
  • 举报
回复
能不能给出实际代码?
allenblade 2008-05-07
  • 打赏
  • 举报
回复
4e00-9fa5编码范围,这个范围里应该都是中文。。。
allenblade 2008-05-07
  • 打赏
  • 举报
回复
可以考虑使用unicode码

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧