diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-03-31 11:43:21 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-03-31 11:43:21 -0400 |
| commit | 8a8b457c98e207d809a7616e73eb59bfe197a7a5 (patch) | |
| tree | 36fcbb7f10e92adbfb866fced7f27af1ef89f636 /BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java | |
| parent | 32b1b46fcc855fffe6b0dddd10442a9a4f1544d2 (diff) | |
More code maintenance
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java | 48 |
1 files changed, 26 insertions, 22 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java index 3ddb8ef..5a8ef8f 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java @@ -19,7 +19,7 @@ public class WeightedRandom<E> { /** * The list of probabilities for each result */ - private FunctionalList<Integer> probs; + private FunctionalList<Integer> probabilities; /** * The list of possible results to pick from @@ -29,7 +29,7 @@ public class WeightedRandom<E> { /** * The source for any needed random numbers */ - private Random src; + private Random source; private int totalChance; @@ -37,14 +37,14 @@ public class WeightedRandom<E> { * Create a new weighted random generator with the specified source of * randomness * - * @param sr + * @param src * The source of randomness to use. */ - public WeightedRandom(Random sr) { - probs = new FunctionalList<>(); + public WeightedRandom(Random src) { + probabilities = new FunctionalList<>(); results = new FunctionalList<>(); - src = sr; + source = src; } /** @@ -52,12 +52,12 @@ public class WeightedRandom<E> { * * @param chance * The chance to get this result. - * @param res + * @param result * The result to get when the chance comes up. */ - public void addProb(int chance, E res) { - probs.add(chance); - results.add(res); + public void addProbability(int chance, E result) { + probabilities.add(chance); + results.add(result); totalChance += chance; } @@ -67,24 +67,28 @@ public class WeightedRandom<E> { * * @return A random value selected in a weighted fashion. */ - public E genVal() { - GenHolder<Integer> v = new GenHolder<>(src.nextInt(totalChance)); - IHolder<E> res = new GenHolder<>(); - GenHolder<Boolean> bl = new GenHolder<>(true); + public E generateValue() { + GenHolder<Integer> randomValue = + new GenHolder<>(source.nextInt(totalChance)); + IHolder<E> currentResult = new GenHolder<>(); + GenHolder<Boolean> valuePicked = new GenHolder<>(true); - probs.forEachIndexed((i, p) -> { - if (bl.unwrap(vl -> vl)) { - if (v.unwrap((vl) -> vl < p)) { - res.transform((vl) -> results.getByIndex(i)); + probabilities.forEachIndexed((itemIndex, itemProbability) -> { + if (valuePicked.unwrap(bool -> bool)) { + if (randomValue + .unwrap((number) -> number < itemProbability)) { + currentResult.transform( + (result) -> results.getByIndex(itemIndex)); - bl.transform((vl) -> false); + valuePicked.transform((bool) -> false); } else { - v.transform((vl) -> vl - p); + randomValue.transform( + (number) -> number - itemProbability); } } }); - return res.unwrap((vl) -> vl); + return currentResult.unwrap((result) -> result); } /** @@ -103,6 +107,6 @@ public class WeightedRandom<E> { * @return A list of pairs of values and value probabilities */ public FunctionalList<Pair<Integer, E>> getValues() { - return probs.pairWith(results); + return probabilities.pairWith(results); } } |
