|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--ec.util.RandomChoice
RandomChoice organizes arrays of floats into distributions which can be used to pick randomly from. You can provide three kinds of arrays:
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.
Constructor Summary | |
RandomChoice()
|
Method Summary | |
static void |
organizeDistribution(double[] probabilities)
Normalizes probabilities, then converts them into continuing sums. |
static void |
organizeDistribution(float[] probabilities)
Normalizes probabilities, then converts them into continuing sums. |
static void |
organizeDistribution(java.lang.Object[] objs,
RandomChoiceChooser chooser)
Normalizes the probabilities associated with an array of objects, then converts them into continuing sums. |
static void |
organizeDistribution(java.lang.Object[] objs,
RandomChoiceChooserD chooser)
Normalizes the probabilities associated with an array of objects, then converts them into continuing sums. |
static int |
pickFromDistribution(double[] probabilities,
double prob,
int checkboundary)
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)
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(java.lang.Object[] objs,
RandomChoiceChooserD chooser,
double prob,
int checkboundary)
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)
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). |
Methods inherited from class java.lang.Object |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait |
Constructor Detail |
public RandomChoice()
Method Detail |
public static void organizeDistribution(float[] probabilities)
public static void organizeDistribution(double[] probabilities)
public static void organizeDistribution(java.lang.Object[] objs, RandomChoiceChooser chooser)
public static void organizeDistribution(java.lang.Object[] objs, RandomChoiceChooserD chooser)
public static int pickFromDistribution(float[] probabilities, float prob, int checkboundary)
public static int pickFromDistribution(double[] probabilities, double prob, int checkboundary)
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, int checkboundary)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |