summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/gen/WeightedRandom.java
diff options
context:
space:
mode:
Diffstat (limited to 'base/src/main/java/bjc/utils/gen/WeightedRandom.java')
-rw-r--r--base/src/main/java/bjc/utils/gen/WeightedRandom.java27
1 files changed, 12 insertions, 15 deletions
diff --git a/base/src/main/java/bjc/utils/gen/WeightedRandom.java b/base/src/main/java/bjc/utils/gen/WeightedRandom.java
index abba36a..be8b8c2 100644
--- a/base/src/main/java/bjc/utils/gen/WeightedRandom.java
+++ b/base/src/main/java/bjc/utils/gen/WeightedRandom.java
@@ -15,7 +15,7 @@ import bjc.utils.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> {
/* The list of probabilities for each result */
@@ -33,13 +33,13 @@ public class WeightedRandom<E> {
* randomness.
*
* @param src
- * The source of randomness to use.
+ * The source of randomness to use.
*/
public WeightedRandom(final Random src) {
probabilities = new FunctionalList<>();
results = 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;
}
@@ -48,10 +48,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) {
probabilities.add(chance);
@@ -63,17 +63,16 @@ public class WeightedRandom<E> {
/**
* Generate a weighted random value.
*
- * @return
- * A random value selected in a weighted fashion.
+ * @return A random value selected in a weighted fashion.
*/
public E generateValue() {
- final IHolder<Integer> value = new Identity<>(source.nextInt(totalChance));
- final IHolder<E> current = new Identity<>();
+ final IHolder<Integer> value = new Identity<>(source.nextInt(totalChance));
+ final IHolder<E> current = new Identity<>();
final IHolder<Boolean> picked = new Identity<>(true);
probabilities.forEachIndexed((index, probability) -> {
- if (picked.unwrap(bool -> bool)) {
- if (value.unwrap((number) -> number < probability)) {
+ if(picked.unwrap(bool -> bool)) {
+ if(value.unwrap((number) -> number < probability)) {
current.transform((result) -> results.getByIndex(index));
picked.transform((bool) -> false);
@@ -89,8 +88,7 @@ public class WeightedRandom<E> {
/**
* Return a list of values that can be generated by this generator
*
- * @return
- * A list of all the values that can be generated
+ * @return A list of all the values that can be generated
*/
public IList<E> getResults() {
return results;
@@ -100,8 +98,7 @@ public class WeightedRandom<E> {
* Return a list containing values that can be generated paired with the
* probability of those values being generated
*
- * @return
- * A list of pairs of values and value probabilities
+ * @return A list of pairs of values and value probabilities
*/
public IList<IPair<Integer, E>> getValues() {
return probabilities.pairWith(results);