Java产生不重复的随机数

阅读 103

2022-06-14

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)

0 0 举报