summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java
diff options
context:
space:
mode:
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.java24
1 files changed, 11 insertions, 13 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 d5256ee..88f623e 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java
@@ -1,17 +1,17 @@
package bjc.utils.gen;
-import java.util.Random;
-
import bjc.utils.data.IHolder;
import bjc.utils.data.IPair;
import bjc.utils.data.Identity;
import bjc.utils.funcdata.FunctionalList;
import bjc.utils.funcdata.IList;
+import java.util.Random;
+
/**
* Represents a random number generator where certain results are weighted more
* heavily than others.
- *
+ *
* @author ben
*
* @param <E>
@@ -38,7 +38,7 @@ public class WeightedRandom<E> {
/**
* Create a new weighted random generator with the specified source of
* randomness
- *
+ *
* @param src
* The source of randomness to use.
*/
@@ -46,16 +46,14 @@ public class WeightedRandom<E> {
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;
}
/**
* Add a probability for a specific result to be given.
- *
+ *
* @param chance
* The chance to get this result.
* @param result
@@ -70,7 +68,7 @@ public class WeightedRandom<E> {
/**
* Generate a weighted random value.
- *
+ *
* @return A random value selected in a weighted fashion.
*/
public E generateValue() {
@@ -79,8 +77,8 @@ public class WeightedRandom<E> {
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);
@@ -95,7 +93,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
*/
public IList<E> getResults() {
@@ -105,7 +103,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
*/
public IList<IPair<Integer, E>> getValues() {