Package squidpony.squidmath
Class WeightedTable
java.lang.Object
squidpony.squidmath.WeightedTable
- All Implemented Interfaces:
Serializable
public class WeightedTable extends Object implements Serializable
A different approach to the same task
Internally, this uses DiverRNG's algorithm as found in
Created by Tommy Ettinger on 1/5/2018.
ProbabilityTable
solves, though this only looks up an appropriate index
instead of also storing items it can choose; allows positive doubles for weights but does not allow nested tables for
simplicity. This doesn't store an RNG (or RandomnessSource) in this class, and instead expects 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 (O(1)
generation time in use, and 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.
Internally, this uses DiverRNG's algorithm as found in
DiverRNG.determineBounded(long, int)
and
DiverRNG.determine(long)
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 given to random(long)
to be just one long.
Created by Tommy Ettinger on 1/5/2018.
- See Also:
- Serialized Form
-
Field Summary
-
Constructor Summary
Constructors Constructor Description WeightedTable()
Constructs a useless WeightedTable that always returns the index 0.WeightedTable(double... probabilities)
Constructs a WeightedTable with the given array of weights for each index. -
Method Summary
Modifier and Type Method Description static WeightedTable
deserializeFromString(String data)
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.String
serializeToString()
-
Field Details
-
Constructor Details
-
WeightedTable
public WeightedTable()Constructs a useless WeightedTable that always returns the index 0. -
WeightedTable
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 doubles, 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 doubles representing the weights for their own indices
-
-
Method Details
-
random
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).- 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
-
serializeToString
-
deserializeFromString
-