public class RandomChoice
extends java.lang.Object
Before the RandomChoice can pick randomly from your array, it must first organize it. It does this by doing the following. First, it normalizes the values in the array. Then it modifies them to their sums. That is, each item i in the array is set to the sum of the original values for items 0...i. If you cannot allow your objects to be modified, then this is not the class for you.
An array is valid if (1) it has no negative values and (2) not all of its values are zero. This RandomChoice code should (I hope) guarantee that an element of zero probability is never returned. RandomChoice uses a binary search to find your index, followed by linear probing (marching up or down the list) to find the first non-zero probability item in the vacinity of that index. As long as there are not a whole lot of zero-valued items in a row, RandomChoice is efficient. You organize your array with organizeDistribution(). Then you can have the RandomChoice pick random items from the array and return their indexes to you. You do this by calling pickFromDistribution(), passing it a random floating point value between 0.0 and 1.0. You call organizeDistribution() only once; after which you may call pickFromDistribution() as many times as you like. You should not modify the array thereafter.
Modifier and Type | Field and Description |
---|---|
static int |
CHECKBOUNDARY |
Constructor and Description |
---|
RandomChoice() |
Modifier and Type | Method and Description |
---|---|
static void |
organizeDistribution(double[] probabilities)
Same as organizeDistribution(probabilities, false);
|
static void |
organizeDistribution(double[] probabilities,
boolean allowAllZeros)
Normalizes probabilities, then converts them into continuing
sums.
|
static void |
organizeDistribution(float[] probabilities)
Same as organizeDistribution(probabilities, false);
|
static void |
organizeDistribution(float[] probabilities,
boolean allowAllZeros)
Normalizes probabilities, then converts them into continuing
sums.
|
static void |
organizeDistribution(java.lang.Object[] objs,
RandomChoiceChooser chooser)
Same as organizeDistribution(objs, chooser, false);
|
static void |
organizeDistribution(java.lang.Object[] objs,
RandomChoiceChooser chooser,
boolean allowAllZeros)
Normalizes the probabilities associated
with an array of objects, then converts them into continuing
sums.
|
static void |
organizeDistribution(java.lang.Object[] objs,
RandomChoiceChooserD chooser)
Same as organizeDistribution(objs, chooser, false);
|
static void |
organizeDistribution(java.lang.Object[] objs,
RandomChoiceChooserD chooser,
boolean allowAllZeros)
Normalizes the probabilities associated
with an array of objects, then converts them into continuing
sums.
|
static int |
pickFromDistribution(double[] probabilities,
double prob)
Picks a random item from an array of probabilities,
normalized and summed as follows: For example,
if four probabilities are {0.3, 0.2, 0.1, 0.4}, then
they should get normalized and summed by the outside owners
as: {0.3, 0.5, 0.6, 1.0}.
|
static int |
pickFromDistribution(double[] probabilities,
double prob,
int checkboundary)
Deprecated.
|
static int |
pickFromDistribution(float[] probabilities,
float prob)
Picks a random item from an array of probabilities,
normalized and summed as follows: For example,
if four probabilities are {0.3, 0.2, 0.1, 0.4}, then
they should get normalized and summed by the outside owners
as: {0.3, 0.5, 0.6, 1.0}.
|
static int |
pickFromDistribution(float[] probabilities,
float prob,
int checkboundary)
Deprecated.
|
static int |
pickFromDistribution(java.lang.Object[] objs,
RandomChoiceChooserD chooser,
double prob)
Picks a random item from an array of objects, each with an
associated probability that is accessed by taking an object
and passing it to chooser.getProbability(obj).
|
static int |
pickFromDistribution(java.lang.Object[] objs,
RandomChoiceChooserD chooser,
double prob,
int checkboundary)
Deprecated.
|
static int |
pickFromDistribution(java.lang.Object[] objs,
RandomChoiceChooser chooser,
float prob)
Picks a random item from an array of objects, each with an
associated probability that is accessed by taking an object
and passing it to chooser.getProbability(obj).
|
static int |
pickFromDistribution(java.lang.Object[] objs,
RandomChoiceChooser chooser,
float prob,
int checkboundary)
Deprecated.
|
public static final int CHECKBOUNDARY
public static void organizeDistribution(float[] probabilities)
public static void organizeDistribution(float[] probabilities, boolean allowAllZeros)
public static void organizeDistribution(double[] probabilities)
public static void organizeDistribution(double[] probabilities, boolean allowAllZeros)
public static void organizeDistribution(java.lang.Object[] objs, RandomChoiceChooser chooser)
public static void organizeDistribution(java.lang.Object[] objs, RandomChoiceChooser chooser, boolean allowAllZeros)
public static void organizeDistribution(java.lang.Object[] objs, RandomChoiceChooserD chooser)
public static void organizeDistribution(java.lang.Object[] objs, RandomChoiceChooserD chooser, boolean allowAllZeros)
public static int pickFromDistribution(float[] probabilities, float prob)
public static int pickFromDistribution(float[] probabilities, float prob, int checkboundary)
public static int pickFromDistribution(double[] probabilities, double prob)
public static int pickFromDistribution(double[] probabilities, double prob, int checkboundary)
public static int pickFromDistribution(java.lang.Object[] objs, RandomChoiceChooser chooser, float prob)
public static int pickFromDistribution(java.lang.Object[] objs, RandomChoiceChooser chooser, float prob, int checkboundary)
public static int pickFromDistribution(java.lang.Object[] objs, RandomChoiceChooserD chooser, double prob)
public static int pickFromDistribution(java.lang.Object[] objs, RandomChoiceChooserD chooser, double prob, int checkboundary)