android - 生成随机数而不重复 [英] android - generate random numbers with no repeat

查看:141
本文介绍了android - 生成随机数而不重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我如何生成随机数而不重复
示例

can anyone please tell me how to generate random numbers with no repeat example

随机(10)应该(可能)返回3,4,2 ,1,7,6,5,8,9,10没有重复

random (10) should(may) return 3,4,2,1,7,6,5,8,9,10 with no repeat

谢谢

推荐答案

我建议将数字添加到 ArrayList< Integer> 然后使用 Collections.shuffle()随机化他们的订单。这样的事情:

I would suggest adding the numbers to an ArrayList<Integer> and then use Collections.shuffle() to randomize their order. Something like this:

ArrayList<Integer> number = new ArrayList<Integer>();
for (int i = 1; i <= 10; ++i) number.add(i);
Collections.shuffle(number);

这篇关于android - 生成随机数而不重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆