随机数问题,请各位帮帮忙!!!

shwmll 2003-12-15 03:03:12
如何从11、40、245、356、358、568、684、968等数中随机取得其中的一个?
...全文
33 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
tsd3698 2003-12-15
  • 打赏
  • 举报
回复
1.int index=(int)(Math.random()*7)

2.import java.util.Random;

Random r = new Random();
int index=r.nextInt(7);

用以上的两种方法可以得到0-7之间的一个随机整型数,用它作为index即可在数组中取数
upcmill 2003-12-15
  • 打赏
  • 举报
回复
灵活实现,先把数字放在树组或者vecter中,然后利用随即函数生成0--7之间的数
swordmen99 2003-12-15
  • 打赏
  • 举报
回复
用ARRAYLIST数组
在用随机函数就可以了
多试试
看看DOSC里的API帮助
很容易
chashui 2003-12-15
  • 打赏
  • 举报
回复
up
sharewarer 2003-12-15
  • 打赏
  • 举报
回复
这个问题不难呀
shwmll 2003-12-15
  • 打赏
  • 举报
回复
修改程序比较难实现呀!
有没有好一点的方法? 或提一些好的建议?
海盗2019 2003-12-15
  • 打赏
  • 举报
回复
1. 先add到ArrayList;
2. 然后随即取一个,并在ArrayList中remove掉;
3. 判断:没有元素了,返回;没取足,返回1,
……
ddbean 2003-12-15
  • 打赏
  • 举报
回复
难道不修改程序知道那些数会变吗?不管是人工修改,还是用代码自动修改,总之你要改
shwmll 2003-12-15
  • 打赏
  • 举报
回复
要取的那些数,随时都可能改变,难道都要修改程序吗?
ddbean 2003-12-15
  • 打赏
  • 举报
回复
还是 xiaohaiz(老土进城,两眼通红) 的方法比较好!
xiaohaiz 2003-12-15
  • 打赏
  • 举报
回复
use my method. you can add or remove Integer elements into or out of List.
Keep the other code and just operate the List.
ddbean 2003-12-15
  • 打赏
  • 举报
回复
就在switch(rand)的case里改啊
shwmll 2003-12-15
  • 打赏
  • 举报
回复
谢谢各位啦!如果要取的数不是固定的,随时改变呢?
有什么高招??
abcpl 2003-12-15
  • 打赏
  • 举报
回复
int[] test = {11,...,968};

java.util.Random ran = new java.util.Random(10L);
int ran_1 = ran.nextInt();
int yournumber = test[ran_1];

or

double d1 = Math.random();
int ran_1 = round(d1*100)%8;
int yournumber = test[ran_e];
xiaohaiz 2003-12-15
  • 打赏
  • 举报
回复
int randomIndex = ran.next(list.size()); -->
int randomIndex = ran.nextInt(list.size());
ddbean 2003-12-15
  • 打赏
  • 举报
回复
先取出0-7的随机数rand,
然后switch(rand),分别为11、40、245、356、358、568、684、968,就可以了。
shwmll 2003-12-15
  • 打赏
  • 举报
回复
各位大虾,帮忙解决一下呀!!!!
xiaohaiz 2003-12-15
  • 打赏
  • 举报
回复
<<
List list = Arrays.asList(new Integer[]{new Integer(11), new Integer(40),
new Integer(245), new Integer(356), new Integer(358), new Integer(568),
new Integer(684), new Integer(968)});

Random ran = new Random(System.currentTimeMills());
int randomIndex = ran.next(list.size());

return ((Integer)list.get(randomIndex)).intValue();
>>
DainelLee 2003-12-15
  • 打赏
  • 举报
回复
int[] a ={11,40,245,356,358,568,684,968};
Random r = new Random();
return a[Math.abs(r.nextInt()) % (a.leng - 1)];

62,614

社区成员

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

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