ReplayablePseudoRandom { public int nextInt(int bound) { return (int) (this.nextDouble() * bound); } ReplayablePseudoRandom();  ReplayablePseudoRandom(double seed); double random(); int nextInt(int bound); double nextDouble(); boolean nextBoolean(); float nextFloat(); double getSeed(); void setSeed(double seed);  }
@Test public void nextIntTestDistribution() { int[] res = new int[6]; int number; for (int i = 0; i < 1000; i++) { number = random.nextInt(6); res[number]++; } assertTrue(res[0] / 1000d > 0.08); assertTrue(res[1] / 1000d > 0.08); assertTrue(res[2] / 1000d > 0.08); assertTrue(res[3] / 1000d > 0.08); assertTrue(res[4] / 1000d > 0.08); assertTrue(res[5] / 1000d > 0.08); }
@Test public void randomShouldBeDifferentWithDifferentSeeds() { double time = System.currentTimeMillis(); double seed1 = time % 100000000; ReplayablePseudoRandom random1 = new ReplayablePseudoRandom(seed1); String firstRandom = ""; for (int i = 0; i < 10; i++) { float number = random1.nextInt(2); firstRandom = firstRandom + number; } double seed2 = (time + 1) % 100000000; ReplayablePseudoRandom random2 = new ReplayablePseudoRandom(seed2); String secondRandom = ""; for (int i = 0; i < 10; i++) { float number = random2.nextInt(2); secondRandom = secondRandom + number; } double seed3 = (time + 2) % 100000000; ReplayablePseudoRandom random3 = new ReplayablePseudoRandom(seed3); String thirdRandom = ""; for (int i = 0; i < 10; i++) { float number = random3.nextInt(2); thirdRandom = thirdRandom + number; } assertNotEquals(firstRandom, secondRandom); assertNotEquals(firstRandom, thirdRandom); assertNotEquals(thirdRandom, secondRandom); }
@Test public void randomShouldBeSameWithDifferentSeeds() { double seed = System.currentTimeMillis() % 100000000; ReplayablePseudoRandom random1 = new ReplayablePseudoRandom(seed); ReplayablePseudoRandom random2 = new ReplayablePseudoRandom(seed); String firstRandom = ""; for (int i = 0; i < 10000; i++) { float number = random1.nextInt(2); firstRandom = firstRandom + number; } String secondRandom = ""; for (int i = 0; i < 10000; i++) { float number = random2.nextInt(2); secondRandom = secondRandom + number; } assertEquals(firstRandom, secondRandom); }