Class WeightedRandomSet<T>

java.lang.Object
toxi.util.datatypes.WeightedRandomSet<T>

public class WeightedRandomSet<T> extends Object
This class provides a generic random-weight distribution of arbitary objects. Add elements with their weight to the set and then use the getRandom() method to retrieve objects. The frequency of returned elements is based on their relative weight. This makes it easy to provide biased preferences. http://www.electricmonk.nl/log/2009/12/23/weighted-random-distribution/
  • Constructor Details

    • WeightedRandomSet

      public WeightedRandomSet()
  • Method Details

    • add

      public WeightedRandomSet<T> add(T item, int weight)
      Add a new element of type T to the set.
      Parameters:
      item -
      weight -
      Returns:
      itself
    • getElements

      public List<WeightedRandomEntry<T>> getElements()
      Returns:
      the elements
    • getRandom

      public T getRandom()
      Returns a randomly picked element from the set. The frequency of occurance depends on the relative weight of each item.
      Returns:
      picked element
    • remove

      public void remove(T item)
      Removes the given item from the set.
      Parameters:
      item -