T
- The type of object to be held in the tablepublic class ProbabilityTable<T>
extends java.lang.Object
implements java.io.Serializable
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()
.Modifier and Type | Field and Description |
---|---|
java.util.ArrayList<ProbabilityTable<T>> |
extraTable
The list of items that can be produced indirectly from
random() (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 from
random() (without additional lookups). |
protected int |
total |
IntVLA |
weights |
Constructor and 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(RandomnessSource rng)
Creates a new probability table with the provided source of randomness
used.
|
ProbabilityTable(java.lang.String seed)
Creates a new probability table with the provided String seed used.
|
Modifier and Type | Method and 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,java.lang.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>,java.lang.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 that
equals(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(java.lang.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(java.lang.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,java.lang.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.
|
java.util.SortedSet<T> |
simpleItems()
Provides a set of the items in this table that are not in nested tables, without
reference to their weight.
|
java.util.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.
|
public final Arrangement<T> table
random()
(without additional lookups).public final java.util.ArrayList<ProbabilityTable<T>> extraTable
random()
(looking up values from inside
the nested tables).public final IntVLA weights
public GWTRNG rng
protected int total
protected int normalTotal
public ProbabilityTable()
public ProbabilityTable(RandomnessSource rng)
rng
- the source of randomnesspublic ProbabilityTable(long seed)
seed
- the RNG seed as a longpublic ProbabilityTable(java.lang.String seed)
seed
- the RNG seed as a Stringpublic T random()
public ProbabilityTable<T> add(T item, int weight)
item
- the object to be addedweight
- the weight to be given to the added objectpublic ProbabilityTable<T> addAll(OrderedMap<T,java.lang.Integer> itemsAndWeights)
OrderedMap.makeMap(Object, Object, Object...)
to produce
the parameter, unless you already have one.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 weightpublic boolean remove(T item)
item
- the item to make less likely or impossiblepublic boolean remove(T item, int weight)
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)public boolean removeAll(java.lang.Iterable<T> items)
items
- an Iterable of T items that will all be removed from the normal (non-nested) items in thispublic boolean removeAll(OrderedMap<T,java.lang.Integer> itemsAndWeights)
OrderedMap.makeMap(Object, Object, Object...)
to produce the parameter, unless you already have one.
Returns true iff the probabilities changed.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 weightpublic ProbabilityTable<T> add(ProbabilityTable<T> table, int weight)
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 tablepublic ProbabilityTable<T> addAllNested(OrderedMap<ProbabilityTable<T>,java.lang.Integer> itemsAndWeights)
OrderedMap.makeMap(Object, Object, Object...)
to produce the
parameter, unless you already have one.
The same rules apply to this as apply to add(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).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 weightpublic int weight(T item)
item
- the item searched forpublic int weight(ProbabilityTable<T> item)
item
- the extra ProbabilityTable to search forpublic OrderedSet<T> items()
StackOverflowError
crashing your program).public java.util.SortedSet<T> simpleItems()
items()
.public java.util.ArrayList<ProbabilityTable<T>> tables()
public void setRandom(GWTRNG random)
random
- an RNG, typically with a seed you want control over; may be a StatefulRNG or some other subclasspublic GWTRNG getRandom()
public ProbabilityTable<T> copy()
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public boolean contentEquals(ProbabilityTable<T> o)
equals(Object)
needs because this always takes a ProbabilityTable.o
- another ProbabilityTablepublic int hashCode()
hashCode
in class java.lang.Object
Copyright © Eben Howard 2012–2022. All rights reserved.