Package redempt.redlib.misc
Class WeightedRandom<T>
java.lang.Object
redempt.redlib.misc.WeightedRandom<T>
- Type Parameters:
T
- The type of the value
Uses a map of outcomes to weights to get random values
-
Constructor Summary
ConstructorDescriptionCreate an empty WeightedRandomWeightedRandom
(Map<T, Integer> weights) Deprecated. -
Method Summary
Modifier and TypeMethodDescriptionclone()
Creates a copy of this WeightedRandomstatic <T,
K> WeightedRandom<T> fromCollection
(Collection<K> collection, Function<K, T> converter, ToDoubleFunction<K> weightGetter) Creates a WeightedRandom from a collectionstatic <T> WeightedRandom<T>
fromDoubleMap
(Map<T, Double> map) Create a new WeightedRandom from a map of outcomes to their weightsstatic <T> WeightedRandom<T>
fromIntMap
(Map<T, Integer> map) Create a new WeightedRandom from a map of outcomes to their weightsstatic <T> WeightedRandom<T>
fromString
(String str, Function<String, T> converter) Deserializes a string to create a WeightedRandomGets the chance each outcome has to occur in percentage (0-100)Gets the map of weights for this WeightedRandomvoid
Removes an outcome from this WeightedRandomroll()
Rolls and gets a weighted random outcomestatic <T> T
Performs a single roll given a map of outcomes to weights.void
Sets another weight in this WeightedRandom, replacing the weight of the outcome if it has already been addedvoid
Sets another weight in this WeightedRandom, replacing the weight of the outcome if it has already been addedConverts this WeightedRandom to a String which can be deserialized later
-
Constructor Details
-
WeightedRandom
Deprecated.UsefromIntMap(Map)
Creates a WeightedRandom using the map of weights- Parameters:
weights
- The map of outcomes to weights
-
WeightedRandom
public WeightedRandom()Create an empty WeightedRandom
-
-
Method Details
-
fromIntMap
Create a new WeightedRandom from a map of outcomes to their weights- Type Parameters:
T
- The type of the outcomes- Parameters:
map
- The map of outcomes to their weights- Returns:
- A WeightedRandom which can be used to roll for the given outcome
-
fromDoubleMap
Create a new WeightedRandom from a map of outcomes to their weights- Type Parameters:
T
- The type of the outcomes- Parameters:
map
- The map of outcomes to their weights- Returns:
- A WeightedRandom which can be used to roll for the given outcome
-
fromCollection
public static <T,K> WeightedRandom<T> fromCollection(Collection<K> collection, Function<K, T> converter, ToDoubleFunction<K> weightGetter) Creates a WeightedRandom from a collection- Type Parameters:
T
- The type the WeightedRandom will roll onK
- The type of the elements in the Collection- Parameters:
collection
- The collection to convertconverter
- The function to convert from the type in the collection to the type in the WeightedRandomweightGetter
- The function to get the weight of an element in the collection- Returns:
- The populated WeightedRandom
-
roll
Rolls and gets a weighted random outcome- Returns:
- A weighted random outcome, or null if there are no possible outcomes
-
getPercentages
Gets the chance each outcome has to occur in percentage (0-100)- Returns:
- A map of each outcome to its percentage chance to occur when calling
roll()
-
getWeights
Gets the map of weights for this WeightedRandom- Returns:
- The weight map
-
set
Sets another weight in this WeightedRandom, replacing the weight of the outcome if it has already been added- Parameters:
outcome
- The weight to setweight
- The outcome to set
-
set
Sets another weight in this WeightedRandom, replacing the weight of the outcome if it has already been added- Parameters:
outcome
- The weight to setweight
- The outcome to set
-
remove
Removes an outcome from this WeightedRandom- Parameters:
outcome
- The outcome to remove
-
clone
Creates a copy of this WeightedRandom -
toString
Converts this WeightedRandom to a String which can be deserialized later- Parameters:
converter
- A function to convert the outcomes of this WeightedRandom to strings which can be deserialized later- Returns:
- The string representing this WeightedRandom
-
fromString
Deserializes a string to create a WeightedRandom- Type Parameters:
T
- The type of the outcomes- Parameters:
str
- The string serialized usingtoString(Function)
converter
- A function to convert the serialized outcomes back to objects- Returns:
- The deserialized WeightedRandom
-
roll
Performs a single roll given a map of outcomes to weights. If you need to roll multiple times, instantiate a WeightedRandom and call roll on that each time instead.- Type Parameters:
T
- The type being returned- Parameters:
map
- The map of outcomes to weights- Returns:
- A weighted random outcome
-
fromIntMap(Map)