Class RandomizedSelection<T extends RandomizedSelection.WeightedObject>

java.lang.Object
se.liu.ida.hefquin.base.utils.RandomizedSelection<T>

public class RandomizedSelection<T extends RandomizedSelection.WeightedObject> extends Object
  • Constructor Details

    • RandomizedSelection

      public RandomizedSelection()
  • Method Details

    • pickOne

      public T pickOne(Set<T> elements)
      Picks one of the elements from the given set by random and returns that element, where the weights of the elements determine their respective probability to be picked. That is, if an element E in the set has a weight of X and the sum of all weights of all elements in the set is Y, then the probability of element E is X/Y. Note that this method needs to populate an internal data structure. Therefore, if you want to pick multiple times (independent from one another), it is better to do so via an iterator that can be obtained with getRandomlyPickingIterator(Set).
    • getRandomlyPickingIterator

      public Iterator<T> getRandomlyPickingIterator(Set<T> elements)
      Returns an infinite iterator that can be used to continuously pick elements from the given set at random, where the weights of the elements determine their respective probability to be picked. That is, if an element E in the set has a weight of X and the sum of all weights of all elements in the set is Y, then the probability of element E is X/Y. The probabilities are not affected by which elements have been picked before.