随机15个不重复数字的算法

宋玮-深圳 2007-09-12 09:50:46
List<xx> candidate = abc.getxxlist();
if (candidate != null && candidate.size() >= 15) {
//取15个随机各不相同的数字
Set<Integer> randomId = new HashSet<Integer>(15);

Random r = new Random();
int max = candidate.size() > 100 ? 100 : candidate.size();

while (randomId.size() < 15) {
randomId.add(r.nextInt(max));
}
List<xx> winner = new ArrayList<xx>(15); //填充15个对象.

for (Iterator<Integer> it = randomId.iterator(); it.hasNext();) {
winner.add(candidate.get(it.next()));
}
...全文
183 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
???

62,614

社区成员

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

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