Class WeightedRandom<T>

java.lang.Object
redempt.redlib.misc.WeightedRandom<T>
Type Parameters:
T - The type of the value

public class WeightedRandom<T> extends Object
Uses a map of outcomes to weights to get random values
  • Constructor Details

    • WeightedRandom

      public WeightedRandom(Map<T,Integer> weights)
      Deprecated.
      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

      public static <T> WeightedRandom<T> fromIntMap(Map<T,Integer> map)
      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

      public static <T> WeightedRandom<T> fromDoubleMap(Map<T,Double> map)
      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 on
      K - The type of the elements in the Collection
      Parameters:
      collection - The collection to convert
      converter - The function to convert from the type in the collection to the type in the WeightedRandom
      weightGetter - The function to get the weight of an element in the collection
      Returns:
      The populated WeightedRandom
    • roll

      public T roll()
      Rolls and gets a weighted random outcome
      Returns:
      A weighted random outcome, or null if there are no possible outcomes
    • getPercentages

      public Map<T,Double> 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

      public Map<T,Double> getWeights()
      Gets the map of weights for this WeightedRandom
      Returns:
      The weight map
    • set

      public void set(T outcome, int weight)
      Sets another weight in this WeightedRandom, replacing the weight of the outcome if it has already been added
      Parameters:
      outcome - The weight to set
      weight - The outcome to set
    • set

      public void set(T outcome, double weight)
      Sets another weight in this WeightedRandom, replacing the weight of the outcome if it has already been added
      Parameters:
      outcome - The weight to set
      weight - The outcome to set
    • remove

      public void remove(T outcome)
      Removes an outcome from this WeightedRandom
      Parameters:
      outcome - The outcome to remove
    • clone

      public WeightedRandom<T> clone()
      Creates a copy of this WeightedRandom
      Overrides:
      clone in class Object
      Returns:
      An identical copy of this WeightedRandom
    • toString

      public String toString(Function<T,String> converter)
      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

      public static <T> WeightedRandom<T> fromString(String str, Function<String,T> converter)
      Deserializes a string to create a WeightedRandom
      Type Parameters:
      T - The type of the outcomes
      Parameters:
      str - The string serialized using toString(Function)
      converter - A function to convert the serialized outcomes back to objects
      Returns:
      The deserialized WeightedRandom
    • roll

      public static <T> T roll(Map<T,Integer> map)
      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