How to generate ten unequal 1-100 integer random numbers with java

Roughly read the answers of other buildings, although most of them can be achieved, but there is no subtlety, and a good program lies in subtlety, so that it can be executed faster. I looked at my 6L. To be honest, his program is not bad, but he ignored one point. His loop is only once, and if the numbers are repeated, it is difficult for SET to store 10 numbers. If you don’t believe me, The landlord can change the 100 in his program to 20 and try, the problem is obvious. 
The following is what I wrote at random. Compared with the 6L, it looks much better.
public class Test2 {
public static void main(String[] args){
Random ran = new Random();
Set <Integer> set = new HashSet<Integer>();
while(set.size()==10?false: true){
int num = ran.nextInt(100)+1;
set.add(num);
}
Iterator<Integer> it = set.iterator();
int count = 0;
while(it.hasNext()){
System .out.println("the "+++count +" random number=="+it.next());
}
}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324737882&siteId=291194637