@Test public void shouldGenerateAbout20PercentOfFirstGenerator() { Generator<Boolean> firstGenerator = new GeneratorOfInstance<>(true); Generator<Boolean> secondGenerator = new GeneratorOfInstance<>(false); int biasTowardsFirst = 20; Generator<Boolean> biasedGenerator = new BiasedGenerator<>(biasTowardsFirst, firstGenerator, secondGenerator); int trueCount = 0; for (int i = 0; i < 10000; i++) { if (biasedGenerator.next()) { trueCount++; } } assertThat(trueCount, greaterThan(1500)); assertThat(trueCount, lessThan(2500)); }
@Override public T next() { return random.nextInt(100) + 1 > percentageBiasTowardsFirst ? secondGenerator.next() : firstGenerator.next(); }
BiasedGenerator implements Generator<T> { @Override public T next() { return random.nextInt(100) + 1 > percentageBiasTowardsFirst ? secondGenerator.next() : firstGenerator.next(); } }
BiasedGenerator implements Generator<T> { @Override public T next() { return random.nextInt(100) + 1 > percentageBiasTowardsFirst ? secondGenerator.next() : firstGenerator.next(); } BiasedGenerator(int percentageBiasTowardsFirst, @Nonnull Generator<T> firstGenerator, @Nonnull Generator<T> secondGenerator); }
BiasedGenerator implements Generator<T> { @Override public T next() { return random.nextInt(100) + 1 > percentageBiasTowardsFirst ? secondGenerator.next() : firstGenerator.next(); } BiasedGenerator(int percentageBiasTowardsFirst, @Nonnull Generator<T> firstGenerator, @Nonnull Generator<T> secondGenerator); @Override T next(); }
BiasedGenerator implements Generator<T> { @Override public T next() { return random.nextInt(100) + 1 > percentageBiasTowardsFirst ? secondGenerator.next() : firstGenerator.next(); } BiasedGenerator(int percentageBiasTowardsFirst, @Nonnull Generator<T> firstGenerator, @Nonnull Generator<T> secondGenerator); @Override T next();  }