Package squidpony.squidmath
Class ProbabilityTable<T>
java.lang.Object
squidpony.squidmath.ProbabilityTable<T>
- Type Parameters:
T
- The type of object to be held in the table
- All Implemented Interfaces:
Serializable
public class ProbabilityTable<T> extends Object implements Serializable
A generic method of holding a probability table to determine weighted random
outcomes.
The weights do not need to add up to any particular value; they will be
normalized when choosing a random entry. This class allows adding
T
items and the weights for
those items after the ProbabilityTable has been constructed with add(Object, int)
or
addAll(OrderedMap)
, as well as removing items entirely with remove(Object)
or
adjusting the weight for an existing item with add(Object, int)
or remove(Object, int)
.
You can also add a nested ProbabilityTable, which has its own weight and can be chosen like any other
item, except it makes its own random choice of its own T
items; you can use the nested table
with add(ProbabilityTable, int)
and addAllNested(OrderedMap)
. Actually getting a
randomly-selected item is easy; just use random()
.- Author:
- Eben Howard - http://squidpony.com - howard@squidpony.com
- See Also:
An alternative for when you want to track the items separately from their weights.
, Serialized Form
-
Field Summary
Fields Modifier and Type Field Description ArrayList<ProbabilityTable<T>>
extraTable
The list of items that can be produced indirectly fromrandom()
(looking up values from inside the nested tables).protected int
normalTotal
GWTRNG
rng
Arrangement<T>
table
The set of items that can be produced directly fromrandom()
(without additional lookups).protected int
total
IntVLA
weights
-
Constructor Summary
Constructors Constructor Description ProbabilityTable()
Creates a new probability table with a random seed.ProbabilityTable(long seed)
Creates a new probability table with the provided long seed used.ProbabilityTable(String seed)
Creates a new probability table with the provided String seed used.ProbabilityTable(RandomnessSource rng)
Creates a new probability table with the provided source of randomness used. -
Method Summary
Modifier and Type Method Description ProbabilityTable<T>
add(ProbabilityTable<T> table, int weight)
Adds the given probability table as a possible set of results for this table.ProbabilityTable<T>
add(T item, int weight)
Adds the given item to the table.ProbabilityTable<T>
addAll(OrderedMap<T,Integer> itemsAndWeights)
Given an OrderedMap of T element keys and Integer weight values, adds all T keys with their corresponding weights into this ProbabilityTable.ProbabilityTable<T>
addAllNested(OrderedMap<ProbabilityTable<T>,Integer> itemsAndWeights)
Given an OrderedMap of ProbabilityTable keys and Integer weight values, adds all keys as nested tables with their corresponding weights into this ProbabilityTable.boolean
contentEquals(ProbabilityTable<T> o)
Can avoid some checks thatequals(Object)
needs because this always takes a ProbabilityTable.ProbabilityTable<T>
copy()
Copies this ProbabilityTable so nothing in the copy is shared with the original, except for the T items (which might not be possible to copy).boolean
equals(Object o)
GWTRNG
getRandom()
Gets the random number generator (a RandomnessSource) this uses.int
hashCode()
OrderedSet<T>
items()
Provides a set of the items in this table, without reference to their weight.T
random()
Returns an object randomly based on assigned weights.boolean
remove(T item)
Removes the possibility of generating the given T item, except by nested ProbabilityTable results.boolean
remove(T item, int weight)
Reduces the likelihood of generating the given T item by the given weight, which can reduce the chance below 0 and thus remove the item entirely.boolean
removeAll(Iterable<T> items)
Given an Iterable of T item keys to remove, this tries to remove each item in items, though it can't affect items in nested ProbabilityTables, and returns true if any probabilities were changed.boolean
removeAll(OrderedMap<T,Integer> itemsAndWeights)
Given an OrderedMap of T item keys and Integer weight values, reduces the weights in this ProbabilityTable for all T keys by their corresponding weights, removing them if the weight becomes 0 or less.void
setRandom(GWTRNG random)
Sets the current random number generator to the given GWTRNG.SortedSet<T>
simpleItems()
Provides a set of the items in this table that are not in nested tables, without reference to their weight.ArrayList<ProbabilityTable<T>>
tables()
Provides a set of the nested ProbabilityTable values in this table, without reference to their weight.int
weight(ProbabilityTable<T> item)
Returns the weight of the extra table if present.int
weight(T item)
Returns the weight of the item if the item is in the table.
-
Field Details
-
table
The set of items that can be produced directly fromrandom()
(without additional lookups). -
extraTable
The list of items that can be produced indirectly fromrandom()
(looking up values from inside the nested tables). -
weights
-
rng
-
total
-
normalTotal
-
-
Constructor Details
-
ProbabilityTable
public ProbabilityTable()Creates a new probability table with a random seed. -
ProbabilityTable
Creates a new probability table with the provided source of randomness used.- Parameters:
rng
- the source of randomness
-
ProbabilityTable
Creates a new probability table with the provided long seed used.- Parameters:
seed
- the RNG seed as a long
-
ProbabilityTable
Creates a new probability table with the provided String seed used.- Parameters:
seed
- the RNG seed as a String
-
-
Method Details
-
random
Returns an object randomly based on assigned weights. Returns null if no elements have been put in the table.- Returns:
- the chosen object or null
-
add
Adds the given item to the table. Weight must be greater than 0.- Parameters:
item
- the object to be addedweight
- the weight to be given to the added object- Returns:
- this for chaining
-
addAll
Given an OrderedMap of T element keys and Integer weight values, adds all T keys with their corresponding weights into this ProbabilityTable. You may want to useOrderedMap.makeMap(Object, Object, Object...)
to produce the parameter, unless you already have one.- Parameters:
itemsAndWeights
- an OrderedMap of T keys to Integer values, where a key will be an item this can retrieve and a value will be its weight- Returns:
- this for chaining
-
remove
Removes the possibility of generating the given T item, except by nested ProbabilityTable results. Returns true iff the item was removed.- Parameters:
item
- the item to make less likely or impossible- Returns:
- true if the probability changed or false if nothing changed
-
remove
Reduces the likelihood of generating the given T item by the given weight, which can reduce the chance below 0 and thus remove the item entirely. Does not affect nested ProbabilityTables. The value for weight must be greater than 0, otherwise this simply returns false without doing anything. Returns true iff the probabilities changed.- Parameters:
item
- the item to make less likely or impossibleweight
- how much to reduce the item's weight by, as a positive non-zero int (greater values here subtract more from the item's weight)- Returns:
- true if the probability changed or false if nothing changed
-
removeAll
Given an Iterable of T item keys to remove, this tries to remove each item in items, though it can't affect items in nested ProbabilityTables, and returns true if any probabilities were changed.- Parameters:
items
- an Iterable of T items that will all be removed from the normal (non-nested) items in this- Returns:
- true if the probabilities changed, or false otherwise
-
removeAll
Given an OrderedMap of T item keys and Integer weight values, reduces the weights in this ProbabilityTable for all T keys by their corresponding weights, removing them if the weight becomes 0 or less. You may want to useOrderedMap.makeMap(Object, Object, Object...)
to produce the parameter, unless you already have one. Returns true iff the probabilities changed.- Parameters:
itemsAndWeights
- an OrderedMap of T keys to Integer values, where a key will be an item that should be reduced in weight or removed and a value will be that item's weight- Returns:
- true if the probabilities changed or false otherwise
-
add
Adds the given probability table as a possible set of results for this table. The table parameter should not be the same object as this ProbabilityTable, nor should it contain cycles that could reference this object from inside the values of table. This could cause serious issues that would eventually terminate in a StackOverflowError if the cycles randomly repeated for too long. Only the first case is checked for (if the contents of this and table are equivalent, it returns without doing anything; this also happens if table is empty or null). Weight must be greater than 0.- Parameters:
table
- the ProbabilityTable to be added; should not be the same as this object (avoid cycles)weight
- the weight to be given to the added table- Returns:
- this for chaining
-
addAllNested
Given an OrderedMap of ProbabilityTable keys and Integer weight values, adds all keys as nested tables with their corresponding weights into this ProbabilityTable. All ProbabilityTable keys should have the same T type as this ProbabilityTable. You may want to useOrderedMap.makeMap(Object, Object, Object...)
to produce the parameter, unless you already have one. The same rules apply to this as apply toadd(ProbabilityTable, int)
; that is, no key in itemsAndWeights can be the same object as this ProbabilityTable, nor should any key contain cycles that could reference this object from inside the values of a key. This could cause serious issues that would eventually terminate in a StackOverflowError if the cycles randomly repeated for too long. Only the first case is checked for (if the contents of this and a key are equivalent, it ignores that key; this also happens if a key is empty or null).- Parameters:
itemsAndWeights
- an OrderedMap of T keys to Integer values, where a key will be an item this can retrieve and a value will be its weight- Returns:
- this for chaining
-
weight
Returns the weight of the item if the item is in the table. Returns zero if the item is not in the table.- Parameters:
item
- the item searched for- Returns:
- the weight of the item, or zero
-
weight
Returns the weight of the extra table if present. Returns zero if the extra table is not present.- Parameters:
item
- the extra ProbabilityTable to search for- Returns:
- the weight of the ProbabilityTable, or zero
-
items
Provides a set of the items in this table, without reference to their weight. Includes nested ProbabilityTable values, but as is the case throughout this class, cyclical references to ProbabilityTable values that reference this table will result in significant issues (such as aStackOverflowError
crashing your program).- Returns:
- an OrderedSet of all items stored; iteration order should be predictable
-
simpleItems
Provides a set of the items in this table that are not in nested tables, without reference to their weight. These are the items that are simple to access, hence the name. If you want the items that are in both the top-level and nested tables, you can useitems()
.- Returns:
- a predictably-ordered set of the items in the top-level table
-
tables
Provides a set of the nested ProbabilityTable values in this table, without reference to their weight. Does not include normal values (non-table); for that, use items().- Returns:
- a "sorted" set of all nested tables stored, really sorted in insertion order
-
setRandom
Sets the current random number generator to the given GWTRNG.- Parameters:
random
- an RNG, typically with a seed you want control over; may be a StatefulRNG or some other subclass
-
getRandom
Gets the random number generator (a RandomnessSource) this uses.- Returns:
- the RandomnessSource used by this class, which is always a GWTRNG
-
copy
Copies this ProbabilityTable so nothing in the copy is shared with the original, except for the T items (which might not be possible to copy). The RNG is also copied.- Returns:
- a copy of this ProbabilityTable; no references should be shared except for T items
-
equals
-
contentEquals
Can avoid some checks thatequals(Object)
needs because this always takes a ProbabilityTable.- Parameters:
o
- another ProbabilityTable- Returns:
- true if both ProbabilityTables are equivalent in contents and likelihoods, not necessarily random state
-
hashCode
-