0
点赞
收藏
分享

微信扫一扫

Java产生不重复的随机数

public class HelloWorld {

public static void main(String[] args) {


Set<Integer>set=new HashSet<>();//数据不会排序
Set<Integer>treeset=new TreeSet<>();//数据会排序
Random r=new Random();
while (set.size()<10)
{
int num=r.nextInt(20)+1;
set.add(num);
}
for(Integer i:set) {
System.out.println(i);
}
}
}


举报

相关推荐

0 条评论