一个关于Random()的问题!!!!!!!!!!!!!!!

nanye18 2003-07-31 10:06:01
class a
{
void selectNo()
{
Random rand=new Random();
text1.setText(""+rand.nextInt(9));
text2.setText(""+rand.nextInt(9));
text3.setText(""+rand.nextInt(9));
text4.setText(""+rand.nextInt(9));
text5.setText(""+rand.nextInt(9));
text6.setText(""+rand.nextInt(9));
}
}
生成3个a类的实例(a1,a2,a3),调用selectNo()这个方法,结果3组随机数都一样,
请问如何生成不同的三组随机数。
...全文
80 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
nanye18 2003-07-31
  • 打赏
  • 举报
回复
good!
Wnyu 2003-07-31
  • 打赏
  • 举报
回复
Random()只是读取widnows随机表, 而这个windows随机表只会在windows启时才会从新填充。
而每次新建立一个实例时, 它又把指针设为零。所以每次新建实例后读随机数, 结果总会一样!
解决方法是:
Random rand=new Random();
随机数 = 当前时间的毫秒数*rand.nextInt(9);
hxzhappy 2003-07-31
  • 打赏
  • 举报
回复
http://www.csdn.net/Develop/article/17%5C17968.shtm

看看

62,614

社区成员

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

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