### Question:
AliasMethodSampler { public int next() { double u = rng.nextDouble() * prob.length; int j = (int) Math.floor(u); if (comparator.compare(u - j, prob[j]) <= 0) { return j; } else { return alias[j]; } } AliasMethodSampler(double[] p);  AliasMethodSampler(double[] p, long seed);  AliasMethodSampler(double[] p, Random rng);  AliasMethodSampler(double[] p, Random rng, double epsilon); int next();  }### Answer:
@Test public void test1() { final long seed = 5; double[] prob = { 0.1, 0.2, 0.3, 0.4 }; AliasMethodSampler am = new AliasMethodSampler(prob, new Random(seed)); int[] counts = new int[4]; for (int i = 0; i < 1000000; i++) { counts[am.next()]++; } assertEquals(100033, counts[0]); assertEquals(200069, counts[1]); assertEquals(299535, counts[2]); assertEquals(400363, counts[3]); }
@Test public void test2() { final long seed = 17; double[] prob = { 0.05, 0.05, 0.05, 0.05, 0.8 }; AliasMethodSampler am = new AliasMethodSampler(prob, new Random(seed)); int[] counts = new int[5]; for (int i = 0; i < 1000000; i++) { counts[am.next()]++; } assertEquals(49949, counts[0]); assertEquals(49726, counts[1]); assertEquals(50441, counts[2]); assertEquals(49894, counts[3]); assertEquals(799990, counts[4]); }