@Test public void multipleChoices() { final Supplier<Integer> s = new RandomPercentageSupplier.Builder<Integer>().withChoice(1, 25.0).withChoice(2, 25.0) .withChoice(3, 25.0).withRandom(new Random()).build(); final Map<Integer, Integer> counts = Maps.newHashMap(); counts.put(1, 0); counts.put(2, 0); counts.put(3, 0); for (int i = 0; i < 100; i++) { final Integer value = s.get(); if (value != null) { counts.put(value, counts.get(value) + 1); } } for (final int count : counts.values()) { assertThat(count, greaterThan(0)); } }
@Override public T get() { final double totalPercent = getCurrentPercents(); final double rnd = this.random.nextDouble() * totalPercent; double previousPercents = 0.0; RandomPercentageSupplier.Choice<T> unUsedChoice = this.choices.get(this.choices.size()-1); for (final RandomPercentageSupplier.Choice<T> choice : this.choices) { if (rnd < previousPercents + choice.currentPercentage) { if (choice.value != null) { return choice.value; } else { return null; } } previousPercents += choice.currentPercentage; } throw new IllegalStateException("Incorrect percentage calculation"); }
RandomPercentageSupplier implements Supplier<T> { @Override public T get() { final double totalPercent = getCurrentPercents(); final double rnd = this.random.nextDouble() * totalPercent; double previousPercents = 0.0; RandomPercentageSupplier.Choice<T> unUsedChoice = this.choices.get(this.choices.size()-1); for (final RandomPercentageSupplier.Choice<T> choice : this.choices) { if (rnd < previousPercents + choice.currentPercentage) { if (choice.value != null) { return choice.value; } else { return null; } } previousPercents += choice.currentPercentage; } throw new IllegalStateException("Incorrect percentage calculation"); } }
RandomPercentageSupplier implements Supplier<T> { @Override public T get() { final double totalPercent = getCurrentPercents(); final double rnd = this.random.nextDouble() * totalPercent; double previousPercents = 0.0; RandomPercentageSupplier.Choice<T> unUsedChoice = this.choices.get(this.choices.size()-1); for (final RandomPercentageSupplier.Choice<T> choice : this.choices) { if (rnd < previousPercents + choice.currentPercentage) { if (choice.value != null) { return choice.value; } else { return null; } } previousPercents += choice.currentPercentage; } throw new IllegalStateException("Incorrect percentage calculation"); } private  RandomPercentageSupplier(final RandomPercentageSupplier.Builder<T> builder); }
RandomPercentageSupplier implements Supplier<T> { @Override public T get() { final double totalPercent = getCurrentPercents(); final double rnd = this.random.nextDouble() * totalPercent; double previousPercents = 0.0; RandomPercentageSupplier.Choice<T> unUsedChoice = this.choices.get(this.choices.size()-1); for (final RandomPercentageSupplier.Choice<T> choice : this.choices) { if (rnd < previousPercents + choice.currentPercentage) { if (choice.value != null) { return choice.value; } else { return null; } } previousPercents += choice.currentPercentage; } throw new IllegalStateException("Incorrect percentage calculation"); } private  RandomPercentageSupplier(final RandomPercentageSupplier.Builder<T> builder); @Override T get(); @Override String toString(); }
RandomPercentageSupplier implements Supplier<T> { @Override public T get() { final double totalPercent = getCurrentPercents(); final double rnd = this.random.nextDouble() * totalPercent; double previousPercents = 0.0; RandomPercentageSupplier.Choice<T> unUsedChoice = this.choices.get(this.choices.size()-1); for (final RandomPercentageSupplier.Choice<T> choice : this.choices) { if (rnd < previousPercents + choice.currentPercentage) { if (choice.value != null) { return choice.value; } else { return null; } } previousPercents += choice.currentPercentage; } throw new IllegalStateException("Incorrect percentage calculation"); } private  RandomPercentageSupplier(final RandomPercentageSupplier.Builder<T> builder); @Override T get(); @Override String toString();  }