fork(1) download
  1. import java.util.*;
  2. import java.util.concurrent.ThreadLocalRandom;
  3. import java.util.function.Consumer;
  4. import java.util.stream.IntStream;
  5.  
  6. class Ideone
  7. {
  8. public static void main (String[] args) throws java.lang.Exception
  9. {
  10. System.out.println("shuffle, all permutations seen after: " + permutations(Collections::shuffle) + " avg");
  11. System.out.println("sort, all permutations seen after: " + permutations(list -> list.sort(randomOrder())) + " avg");
  12. }
  13.  
  14. private static double permutations(Consumer<List<String>> shuffle) {
  15. final int expectedPermutations = 6;
  16. return IntStream.generate(() -> {
  17. int steps;
  18. HashSet<List<?>> seen = new HashSet<>();
  19. for(steps = 0; seen.size() < expectedPermutations; steps++) {
  20. List<String> l = Arrays.asList("a", "b", "c");
  21. shuffle.accept(l);
  22. seen.add(l);
  23. }
  24. return steps;
  25. }).limit(1000).average().orElse(0);
  26. }
  27.  
  28. public static Comparator<String> randomOrder() {
  29. ThreadLocalRandom r = ThreadLocalRandom.current();
  30. int x = r.nextInt(), y = r.nextInt(), z = r.nextInt();
  31. boolean b = r.nextBoolean();
  32. return Comparator.comparingInt((String s) -> Integer.reverse((s.hashCode()&x)^y))
  33. .thenComparingInt(s -> s.length()^z)
  34. .thenComparing(b? Comparator.naturalOrder(): Comparator.reverseOrder());
  35. }
  36. }
Success #stdin #stdout 0.76s 64900KB
stdin
Standard input is empty
stdout
shuffle, all permutations seen after: 14.489 avg
sort, all permutations seen after: 16.429 avg