@Test @SuppressWarnings("ArraysAsListWithZeroOrOneArgument") public void testRandomness() { int entriesCount = 5000000; int keysCount = 10; Host host1 = new Host("host_1", 4444, keysCount - 1); List<Host> hosts = new ArrayList<>(keysCount); hosts.add(host1); int i = keysCount; while (i --> 1) { hosts.add(newHost()); } HashMap<Host, Integer> appearances = new HashMap<>(keysCount, entriesCount / keysCount); RandomHostSelectionStrategy strategy = new RandomHostSelectionStrategy(); i = entriesCount; while (i-- > 0) { Host host = strategy.selectRandom(hosts); appearances.put(host, Optional.ofNullable(appearances.get(host)).orElse(0) + 1); } assertThat(appearances.remove(host1), isAround(entriesCount / 2)); for (int count : appearances.values()) { assertThat(count, isAround(entriesCount / 2 / (keysCount - 1))); } }
protected <T extends WithCount> T selectRandom(List<T> elements) { List<T> copies = new ArrayList<>(); for (T element : elements) { copies.addAll(nCopies(element.getCount(), element)); } shuffle(copies); return copies.get(0); }
RandomHostSelectionStrategy implements HostSelectionStrategy { protected <T extends WithCount> T selectRandom(List<T> elements) { List<T> copies = new ArrayList<>(); for (T element : elements) { copies.addAll(nCopies(element.getCount(), element)); } shuffle(copies); return copies.get(0); } }
RandomHostSelectionStrategy implements HostSelectionStrategy { protected <T extends WithCount> T selectRandom(List<T> elements) { List<T> copies = new ArrayList<>(); for (T element : elements) { copies.addAll(nCopies(element.getCount(), element)); } shuffle(copies); return copies.get(0); }  }
RandomHostSelectionStrategy implements HostSelectionStrategy { protected <T extends WithCount> T selectRandom(List<T> elements) { List<T> copies = new ArrayList<>(); for (T element : elements) { copies.addAll(nCopies(element.getCount(), element)); } shuffle(copies); return copies.get(0); }  @Override Region selectRegion(List<Region> allRegions, List<Region> unvisitedRegions); @Override Host selectHost(List<Host> hosts); }
RandomHostSelectionStrategy implements HostSelectionStrategy { protected <T extends WithCount> T selectRandom(List<T> elements) { List<T> copies = new ArrayList<>(); for (T element : elements) { copies.addAll(nCopies(element.getCount(), element)); } shuffle(copies); return copies.get(0); }  @Override Region selectRegion(List<Region> allRegions, List<Region> unvisitedRegions); @Override Host selectHost(List<Host> hosts);  }