Shotgun implements PatternStrategy { @Override public Iterable<Integer> nextIndices() { SingleReaderArrayIterable iterable = new SingleReaderArrayIterable(serviceCount); Integer[] orderToTry = iterable.getIndices(); System.arraycopy(serviceIndices, 0, orderToTry, 0, serviceCount); shuffle(orderToTry); return iterable; } Shotgun(int serviceCount, int acquireCount); @Override Iterable<Integer> nextIndices(); @Override int acquireCount();  }
@Test public void indicesAreReturnedUniformly() { Shotgun shotgun = new Shotgun(3, 2); int firstTotal = 0; int secondTotal = 0; int thirdTotal = 0; for (int i = 0; i < 5000; ++i) { Iterator<Integer> indices = shotgun.nextIndices().iterator(); int first = indices.next(); int second = indices.next(); int third = indices.next(); assertFalse(indices.hasNext()); assertNotEquals(first, second); assertNotEquals(first, third); assertNotEquals(second, third); firstTotal += first; secondTotal += second; thirdTotal += third; } double firstMean = (double) firstTotal / 5000; double secondMean = (double) secondTotal / 5000; double thirdMean = (double) thirdTotal / 5000; String message = "Concerning distribution of indices returned"; assertTrue(message, 0.15 > Math.abs(firstMean - secondMean)); assertTrue(message, 0.15 > Math.abs(firstMean - thirdMean)); assertTrue(message, 0.15 > Math.abs(secondMean - thirdMean)); }