Class WeightedChance<E>
java.lang.Object
me.sciguymjm.uberenchant.api.utils.random.WeightedChance<E>
- Type Parameters:
E- The type of value (Can be anything)
Simple utility class for weighted chances
Example Usage:
Example Usage:
WeightedChance<Boolean> wc = new WeightedChance<Boolean>(); // Create new WeightedChance for booleans
wc.add(9.0, false); // False 90% of the time (a 9 out of 10 chance)
wc.add(1.0, true); // True 10% of the time (a 1 out of 10 chance)
System.out.println(wc.next()); // Print the value
-
Constructor Summary
ConstructorsConstructorDescriptionConstructor for a WeightedChanceWeightedChance(Collection<? extends Weighted<E>> collection) Constructs a WeightedChance using the specified collectionWeightedChance(Random random) Constructor for a WeightedChance -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdd a new possible selection with a weight.booleanAdd a new possible selection.voidaddAll(Collection<? extends Weighted<E>> collection) Adds all the items in the specified collection to this WeightedChancebooleanChecks if this WeightedChance contains the specified value or not.booleanChecks if this WeightedChance contains the specified entry or not.static <E> WeightedChance<E>fromCollection(Collection<? extends Weighted<E>> collection) Returns a new instance of a WeightedChance using specified collection
Same as usingWeightedChance(Collection)doublegetTotal()Gets the total weight of all the valuesdoubleGets the weight of the specified value.booleanRemove possible selections if the value exists.booleanRemove possible selections based on a predicate.select()Selects the next possible value.static <E> Eselect(Collection<? extends Weighted<E>> collection) Static method to select a random weighted item from a collectionSelects the next possible value using specified random.static <E> Eselect(Random random, Collection<? extends Weighted<E>> collection) Static method to select a random weighted item from a collection using a random
-
Constructor Details
-
WeightedChance
public WeightedChance()Constructor for a WeightedChance -
WeightedChance
Constructor for a WeightedChance -
WeightedChance
Constructs a WeightedChance using the specified collection- Parameters:
collection- A collection of items that implement theWeightedinterface
-
-
Method Details
-
fromCollection
Returns a new instance of a WeightedChance using specified collection
Same as usingWeightedChance(Collection)- Type Parameters:
E- The type of value (Can be anything)- Parameters:
collection- A collection of items that implement theWeightedinterface- Returns:
- A new WeightedChance using data from the specified collection
-
select
Static method to select a random weighted item from a collection- Type Parameters:
E- The type of value- Parameters:
collection- The collection- Returns:
- A randomly selected entry based on weight
-
select
Static method to select a random weighted item from a collection using a random- Type Parameters:
E- The type of value- Parameters:
random- The random to use (Good for setting a seed)collection- The collection- Returns:
- A randomly selected entry based on weight
-
add
Add a new possible selection with a weight.- Parameters:
value- The valueweight- The weight- Returns:
- True if it was successfully added
-
add
Add a new possible selection.- Parameters:
entry- The selection (Implements theWeightedinterface)- Returns:
- True if it was successfully added
-
addAll
Adds all the items in the specified collection to this WeightedChance- Parameters:
collection- The collection to add
-
remove
Remove possible selections if the value exists.- Parameters:
value- The value to remove- Returns:
- True if any were removed
-
remove
Remove possible selections based on a predicate.- Parameters:
predicate- The predicate to use- Returns:
- True if any were removed
-
select
Selects the next possible value.- Returns:
- The next value
-
select
Selects the next possible value using specified random.- Parameters:
random- The Random to use- Returns:
- The next value
-
getWeight
Gets the weight of the specified value.- Parameters:
value- The value to get the weight for- Returns:
- The weight or 0 if not found
-
getTotal
public double getTotal()Gets the total weight of all the values- Returns:
- Total weight
-
contains
Checks if this WeightedChance contains the specified value or not.- Parameters:
value- the value to check for- Returns:
- True if this WeightedChance contains the value
-
contains
Checks if this WeightedChance contains the specified entry or not.- Parameters:
weighted- the entry to check for- Returns:
- True if this WeightedChance contains the value
-