I have a simple program that prints unique values from a specific range.
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
public class Zad3 {
public static void main(String[] args) {
System.out.println(numbers(5));
}
private static Set<Integer> numbers(int howMany) {
Random rand = new Random();
Set<Integer> numb = new HashSet<>();
while (numb.size() < howMany) {
numb.add(rand.nextInt(5) + 1);
}
return numb;
}
}
When I set rand to 1-5 it prints [1, 2, 3, 4, 5] but when I set rand to 1-100 then the outcome is not sorted - ie. [53, 86, 39, 91, 60]. Why is it sorted in 1st example and not in 2nd?