Class WeightedTable
java.lang.Object
com.github.yellowstonegames.core.WeightedTable
A different approach to the same task as a probability table, though this only looks up an appropriate index
instead of also storing items it can choose; allows positive floats for weights but does not allow nested tables for
simplicity. This doesn't store a Random in this class, and instead expects either a Random as a parameter, or a long
to be given for each random draw from the table (these long parameters can be random, sequential, or in some other
way different every time).
Uses Vose's Alias Method, and is based fairly-closely on the code given by Keith Schwarz at that link. Because Vose's Alias Method is remarkably fast (it takes O(1) time to get a random index, and takes O(n) time to construct a WeightedTable instance), this may be useful to consider if you don't need all the features of ProbabilityTable or if you want deeper control over the random aspects of it.
Uses Vose's Alias Method, and is based fairly-closely on the code given by Keith Schwarz at that link. Because Vose's Alias Method is remarkably fast (it takes O(1) time to get a random index, and takes O(n) time to construct a WeightedTable instance), this may be useful to consider if you don't need all the features of ProbabilityTable or if you want deeper control over the random aspects of it.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a useless WeightedTable that always returns the index 0.WeightedTable(float... probabilities) Constructs a WeightedTable with the given array of weights for each index.WeightedTable(float[] probabilities, int offset, int length) Constructs a WeightedTable with the given array of weights for each index.WeightedTable(com.github.tommyettinger.ds.FloatList probabilities) Constructs a WeightedTable with the given array of weights for each index.WeightedTable(WeightedTable other) Copy constructor; avoids sharing any state between this and the original. -
Method Summary
Modifier and TypeMethodDescriptioncopy()Copies this WeightedTable; avoids sharing any state between this and the copy.booleanintgetSize()inthashCode()intrandom(long state) Gets an index of one of the weights in this WeightedTable, with the choice determined deterministically by the given long, but higher weights will be returned by more possible inputs than lower weights.intGets an index of one of the weights in this WeightedTable, with the choice determined by the given random number generator, but higher weights will be returned more frequently than lower weights.static WeightedTablestringDeserialize(String data)
-
Field Details
-
mixed
protected int[] mixed -
size
protected int size
-
-
Constructor Details
-
WeightedTable
public WeightedTable()Constructs a useless WeightedTable that always returns the index 0. -
WeightedTable
public WeightedTable(float... probabilities) Constructs a WeightedTable with the given array of weights for each index. The array can also be a varargs for convenience. The weights can be any positive non-zero floats, but should usually not be so large or small that precision loss is risked. Each weight will be used to determine the likelihood of that weight's index being returned byrandom(long).- Parameters:
probabilities- an array or varargs of positive floats representing the weights for their own indices
-
WeightedTable
public WeightedTable(com.github.tommyettinger.ds.FloatList probabilities) Constructs a WeightedTable with the given array of weights for each index. The array can also be a varargs for convenience. The weights can be any positive non-zero floats, but should usually not be so large or small that precision loss is risked. Each weight will be used to determine the likelihood of that weight's index being returned byrandom(long).- Parameters:
probabilities- a FloatList of positive floats representing the weights for their own indices
-
WeightedTable
public WeightedTable(float[] probabilities, int offset, int length) Constructs a WeightedTable with the given array of weights for each index. The array can also be a varargs for convenience. The weights can be any positive non-zero floats, but should usually not be so large or small that precision loss is risked. Each weight will be used to determine the likelihood of that weight's index being returned byrandom(long).- Parameters:
probabilities- an array or varargs of positive floats representing the weights for their own indices
-
WeightedTable
Copy constructor; avoids sharing any state between this and the original.- Parameters:
other- another WeightedTable to copy; no state will be shared
-
-
Method Details
-
copy
Copies this WeightedTable; avoids sharing any state between this and the copy.- Returns:
- an exact copy of this WeightedTable
-
random
public int random(long state) Gets an index of one of the weights in this WeightedTable, with the choice determined deterministically by the given long, but higher weights will be returned by more possible inputs than lower weights. The state parameter can be from a random source, but this will randomize it again anyway, so it is also fine to just give sequential longs. The important thing is that each state input this is given will produce the same result for this WeightedTable every time, so you should give different state values when you want random-seeming results. You may want to call this likeweightedTable.random(++state), where state is a long, to ensure the inputs change. This will always return an int between 0 (inclusive) andsize(exclusive).
Internally, this uses a unary hash (a function that converts one number to a random-seeming but deterministic other number) to generate two ints, one used for probability and treated as a 31-bit integer and the other used to determine the chosen column, which is bounded to an arbitrary positive int. It does this with just one randomized 64-bit value, allowing the state parameter to be just one long.
You can also userandom(Random)to avoid handling state directly; this can be faster if using a particularly fast Random implementation such as one from juniper.- Parameters:
state- a long that should be different every time; consider calling with++state- Returns:
- a random-seeming index from 0 to
size- 1, determined by weights and the given state
-
random
Gets an index of one of the weights in this WeightedTable, with the choice determined by the given random number generator, but higher weights will be returned more frequently than lower weights. The rng parameter can be any implementation of Random, and some implementations are likely to be faster than the unary hash used byrandom(long). This will return an int between 0 (inclusive) andsize(exclusive).- Parameters:
rng- an Random; itsRandom.nextLong()method will be called once- Returns:
- a random index from 0 to
size- 1, determined by weights and the given rng
-
stringSerialize
-
stringDeserialize
-
equals
-
hashCode
-
getSize
public int getSize()
-