Package toxi.util.datatypes
Class WeightedRandomSet<T>
java.lang.Object
toxi.util.datatypes.WeightedRandomSet<T>
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
-
Method Summary
Modifier and TypeMethodDescriptionAdd a new element of type T to the set.Returns a randomly picked element from the set.void
Removes the given item from the set.
-
Constructor Details
-
WeightedRandomSet
public WeightedRandomSet()
-
-
Method Details
-
add
Add a new element of type T to the set.- Parameters:
item
-weight
-- Returns:
- itself
-
getElements
- Returns:
- the elements
-
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
Removes the given item from the set.- Parameters:
item
-
-