toxi.util.datatypes
Class WeightedRandomSet<T>

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

public class WeightedRandomSet<T>
extends java.lang.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 Summary
WeightedRandomSet()
           
 
Method Summary
 WeightedRandomSet<T> add(T item, int weight)
          Add a new element of type T to the set.
 java.util.List<WeightedRandomEntry<T>> getElements()
           
 T getRandom()
          Returns a randomly picked element from the set.
 void remove(T item)
          Removes the given item from the set.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WeightedRandomSet

public WeightedRandomSet()
Method Detail

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 java.util.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 -