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.java39
1 files changed, 18 insertions, 21 deletions
diff --git a/base/src/main/java/bjc/utils/gen/WeightedRandom.java b/base/src/main/java/bjc/utils/gen/WeightedRandom.java
index 18225ef..abba36a 100644
--- a/base/src/main/java/bjc/utils/gen/WeightedRandom.java
+++ b/base/src/main/java/bjc/utils/gen/WeightedRandom.java
@@ -15,32 +15,25 @@ 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
- */
+ /* The list of probabilities for each result */
private final IList<Integer> probabilities;
-
- /*
- * The list of possible results to pick from
- */
+ /* The list of possible results to pick from */
private final IList<E> results;
- /*
- * The source for any needed random numbers
- */
+ /* The source for any needed random numbers */
private final Random source;
-
+ /* The total chance for all values. */
private int totalChance;
/**
* Create a new weighted random generator with the specified source of
- * randomness
+ * randomness.
*
* @param src
- * The source of randomness to use.
+ * The source of randomness to use.
*/
public WeightedRandom(final Random src) {
probabilities = new FunctionalList<>();
@@ -55,9 +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);
@@ -69,11 +63,12 @@ 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) -> {
@@ -94,7 +89,8 @@ 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;
@@ -104,7 +100,8 @@ 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);