HashSet<Integer> hs = new HashSet<>(); while (hs.size() < 10) { int x = (int) (Math.random()*20) + 1; hs.add(x); } for (Integer i : hs) { System.out.println(i); }