@Test public void oneChoice() { final Supplier<Integer> s = new RandomSupplier.Builder<Integer>().withChoice(1).build(); for (int i = 0; i < 10; i++) { assertThat(s.get(), is(1)); } }
@Override public T get() { final double totalWeight = getCurrentWeights(); final double rnd = this.random.nextDouble() * totalWeight; double previousWeights = 0.0; for (final Choice<T> choice : this.choices) { if (rnd < previousWeights + choice.currentWeight) { return choice.value; } previousWeights += choice.currentWeight; } throw new IllegalStateException("Incorrect weight calculation"); }
RandomSupplier implements Supplier<T> { @Override public T get() { final double totalWeight = getCurrentWeights(); final double rnd = this.random.nextDouble() * totalWeight; double previousWeights = 0.0; for (final Choice<T> choice : this.choices) { if (rnd < previousWeights + choice.currentWeight) { return choice.value; } previousWeights += choice.currentWeight; } throw new IllegalStateException("Incorrect weight calculation"); } }
RandomSupplier implements Supplier<T> { @Override public T get() { final double totalWeight = getCurrentWeights(); final double rnd = this.random.nextDouble() * totalWeight; double previousWeights = 0.0; for (final Choice<T> choice : this.choices) { if (rnd < previousWeights + choice.currentWeight) { return choice.value; } previousWeights += choice.currentWeight; } throw new IllegalStateException("Incorrect weight calculation"); } private  RandomSupplier(final Builder<T> builder); }
RandomSupplier implements Supplier<T> { @Override public T get() { final double totalWeight = getCurrentWeights(); final double rnd = this.random.nextDouble() * totalWeight; double previousWeights = 0.0; for (final Choice<T> choice : this.choices) { if (rnd < previousWeights + choice.currentWeight) { return choice.value; } previousWeights += choice.currentWeight; } throw new IllegalStateException("Incorrect weight calculation"); } private  RandomSupplier(final Builder<T> builder); @Override T get(); @Override String toString(); }
RandomSupplier implements Supplier<T> { @Override public T get() { final double totalWeight = getCurrentWeights(); final double rnd = this.random.nextDouble() * totalWeight; double previousWeights = 0.0; for (final Choice<T> choice : this.choices) { if (rnd < previousWeights + choice.currentWeight) { return choice.value; } previousWeights += choice.currentWeight; } throw new IllegalStateException("Incorrect weight calculation"); } private  RandomSupplier(final Builder<T> builder); @Override T get(); @Override String toString();  }