import java.util.Collections; import java.util.List; import com.google.common.collect.Lists; import com.google.common.primitives.Ints; public class IntArray { public static int[] ofSize(int size) { if (size < 0) throw new IllegalArgumentException( String.format("Expected a size [%d] to be at least 0", size)); List ints = Lists.newArrayList(); for (int n = 0; n < size; n++) ints.add(n); Collections.shuffle(ints); return Ints.toArray(ints); } } import java.util.Random; import org.junit.Test; public class IntArrayTest { @Test public void ofSizeIsRandom() { Random randomizer = new Random(); int size = randomizer.nextInt(1000) + 2; int index1 = randomizer.nextInt(size), index2; do index2 = randomizer.nextInt(size); while (index1 == index2); int value1 = randomizer.nextInt(size), value2; do value2 = randomizer.nextInt(size); while (value1 == value2); int round = 1; do { int[] ints = IntArray.ofSize(size); if (ints[index1] == value1 && ints[index2] == value2) break; int threshold = size * (size - 1); if (round % threshold == 0) { System.out.println("perhaps the function does not produce random arrays"); } round++; } while (true); } }