diff options
Diffstat (limited to 'base/src/main/java/bjc/utils/gen/WeightedRandom.java')
| -rw-r--r-- | base/src/main/java/bjc/utils/gen/WeightedRandom.java | 104 |
1 files changed, 54 insertions, 50 deletions
diff --git a/base/src/main/java/bjc/utils/gen/WeightedRandom.java b/base/src/main/java/bjc/utils/gen/WeightedRandom.java index 15f5821..b17017b 100644 --- a/base/src/main/java/bjc/utils/gen/WeightedRandom.java +++ b/base/src/main/java/bjc/utils/gen/WeightedRandom.java @@ -14,7 +14,7 @@ import bjc.funcdata.IList; * @author ben * * @param <E> - * The type of values that are randomly selected. + * The type of values that are randomly selected. */ public class WeightedRandom<E> { private final IList<IPair<Integer, E>> values; @@ -33,12 +33,13 @@ public class WeightedRandom<E> { * randomness. * * @param src - * The source of randomness to use. + * The source of randomness to use. */ public WeightedRandom(Random src) { values = new FunctionalList<>(); - if (src == null) throw new NullPointerException("Source of randomness must not be null"); + if (src == null) + throw new NullPointerException("Source of randomness must not be null"); source = src; } @@ -62,10 +63,10 @@ public class WeightedRandom<E> { * Add a probability for a specific result to be given. * * @param chance - * The chance to get this result. + * The chance to get this result. * * @param result - * The result to get when the chance comes up. + * The result to get when the chance comes up. */ public void addProbability(final int chance, final E result) { values.add(new Pair<>(chance, result)); @@ -84,9 +85,9 @@ public class WeightedRandom<E> { /** * Generate a random value, using the specified Random. - * + * * @param rn - * The Random instance to use. + * The Random instance to use. * @return A random value. */ public E generateValue(Random rn) { @@ -130,13 +131,12 @@ public class WeightedRandom<E> { /** * Get a descending value. - * - * Descending values are quite simple. You have a 1 in factor chance to - * advance to the next value, otherwise, the current value is the one - * returned. - * + * + * Descending values are quite simple. You have a 1 in factor chance to advance + * to the next value, otherwise, the current value is the one returned. + * * @param factor - * The descent factor to use. + * The descent factor to use. * @return A random value. */ public E getDescent(int factor) { @@ -145,22 +145,23 @@ public class WeightedRandom<E> { /** * Get a descending value. - * - * Descending values are quite simple. You have a 1 in factor chance to - * advance to the next value, otherwise, the current value is the one - * returned. - * + * + * Descending values are quite simple. You have a 1 in factor chance to advance + * to the next value, otherwise, the current value is the one returned. + * * @param factor - * The descent factor to use. + * The descent factor to use. * @param rn - * The Random instance to use. + * The Random instance to use. * @return A random value. */ public E getDescent(int factor, Random rn) { - if (values.getSize() == 0) return null; + if (values.getSize() == 0) + return null; for (IPair<Integer, E> val : values) { - if (rn.nextInt(factor) == 0) continue; + if (rn.nextInt(factor) == 0) + continue; if (exhaust) { totalChance -= val.getLeft(); @@ -172,25 +173,25 @@ public class WeightedRandom<E> { } IPair<Integer, E> val = values.getByIndex(values.getSize() - 1); - if (exhaust) values.removeMatching(val); + if (exhaust) + values.removeMatching(val); return val.getRight(); } /** * Get a value, treating this as a set of binomial trials. - * - * Essentially, the system rolls a bound-sided dice trials times, and - * marks a success for every roll less than or equal to target. - * + * + * Essentially, the system rolls a bound-sided dice trials times, and marks a + * success for every roll less than or equal to target. + * * @param target - * The number to roll at or under. + * The number to roll at or under. * @param bound - * The maximum roll value. + * The maximum roll value. * @param trials - * The number of times to roll. - * @return The value at the index corresponding to the number of - * successes. + * The number of times to roll. + * @return The value at the index corresponding to the number of successes. */ public E getBinomial(int target, int bound, int trials) { return getBinomial(target, bound, trials, source); @@ -198,41 +199,43 @@ public class WeightedRandom<E> { /** * Get a value, treating this as a set of binomial trials. - * - * Essentially, the system rolls a bound-sided dice trials times, and - * marks a success for every roll less than or equal to target. - * + * + * Essentially, the system rolls a bound-sided dice trials times, and marks a + * success for every roll less than or equal to target. + * * @param target - * The number to roll at or under. + * The number to roll at or under. * @param bound - * The maximum roll value. + * The maximum roll value. * @param trials - * The number of times to roll. + * The number of times to roll. * @param rn - * The Random instance to use. - * @return The value at the index corresponding to the number of - * successes. + * The Random instance to use. + * @return The value at the index corresponding to the number of successes. */ public E getBinomial(int target, int bound, int trials, Random rn) { - if (values.getSize() == 0) return null; + if (values.getSize() == 0) + return null; int numSuc = 0; for (int i = 0; i < trials; i++) { /* - * Adjust for zero, because it's easy to think of this - * as rolling a bound-sided dice and marking a success - * for every roll less than or equal to target. + * Adjust for zero, because it's easy to think of this as rolling a + * bound-sided dice and marking a success for every roll less than or equal to + * target. */ int num = rn.nextInt(bound) + 1; if (num <= target) { - //System.err.printf("\t\tTRACE: mark binomial success (%d <= 1d%d, %d)\n", target, bound, num); + // System.err.printf("\t\tTRACE: mark binomial success (%d <= 1d%d, + // %d)\n", target, bound, num); numSuc += 1; } } - //System.err.printf("\tTRACE: got %d success for binomial trials (%d <= 1d%d, %d times)\n", numSuc, target, bound, trials); + // System.err.printf("\tTRACE: got %d success for binomial trials (%d <= 1d%d, + // %d times)\n", numSuc, target, bound, trials); IPair<Integer, E> val = values.getByIndex(Math.min(numSuc, values.getSize() - 1)); if (exhaust) { totalChance -= val.getLeft(); @@ -244,8 +247,9 @@ public class WeightedRandom<E> { } /** - * Return a new WeightedRandom that is exhaustible (only returns one value once). - * + * Return a new WeightedRandom that is exhaustible (only returns one value + * once). + * * @return A new WeightedRandom that is exhaustible. */ public WeightedRandom<E> exhaustible() { |
