Class RandomChoice
- An array of floats
- An array of doubles
- An array of arbitrary objects, plus a RandomChoiceChooser which knows how to get and set the appropriate "float" value of objects in this array.
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.
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic 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
(Object[] objs, RandomChoiceChooser chooser) Same as organizeDistribution(objs, chooser, false);static void
organizeDistribution
(Object[] objs, RandomChoiceChooserD chooser) Same as organizeDistribution(objs, chooser, false);static void
organizeDistribution
(Object[] objs, RandomChoiceChooserD chooser, boolean allowAllZeros) Normalizes the probabilities associated with an array of objects, then converts them into continuing sums.static void
organizeDistribution
(Object[] objs, RandomChoiceChooser 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) 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) 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
(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
(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
(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
(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).
-
Field Details
-
CHECKBOUNDARY
public static final int CHECKBOUNDARY- See Also:
-
-
Constructor Details
-
RandomChoice
public RandomChoice()
-
-
Method Details
-
organizeDistribution
public static void organizeDistribution(float[] probabilities) Same as organizeDistribution(probabilities, false); -
organizeDistribution
public static void organizeDistribution(float[] probabilities, boolean allowAllZeros) Normalizes probabilities, then converts them into continuing sums. This prepares them for being usable in pickFromDistribution. If the probabilities are all 0, then selection is uniform, unless allowAllZeros is false, in which case an ArithmeticException is thrown. If any of them are negative, or if the distribution is empty, then an ArithmeticException is thrown. For example, {0.6, 0.4, 0.2, 0.8} -> {0.3, 0.2, 0.1, 0.4} -> {0.3, 0.5, 0.6, 1.0} -
organizeDistribution
public static void organizeDistribution(double[] probabilities) Same as organizeDistribution(probabilities, false); -
organizeDistribution
public static void organizeDistribution(double[] probabilities, boolean allowAllZeros) Normalizes probabilities, then converts them into continuing sums. This prepares them for being usable in pickFromDistribution. If the probabilities are all 0, then selection is uniform, unless allowAllZeros is false, in which case an ArithmeticException is thrown. If any of them are negative, or if the distribution is empty, then an ArithmeticException is thrown. For example, {0.6, 0.4, 0.2, 0.8} -> {0.3, 0.2, 0.1, 0.4} -> {0.3, 0.5, 0.6, 1.0} -
organizeDistribution
Same as organizeDistribution(objs, chooser, false); -
organizeDistribution
public static void organizeDistribution(Object[] objs, RandomChoiceChooser chooser, boolean allowAllZeros) Normalizes the probabilities associated with an array of objects, then converts them into continuing sums. This prepares them for being usable in pickFromDistribution. If the probabilities are all 0, then selection is uniform, unless allowAllZeros is false, in which case an ArithmeticException is thrown. If any of them are negative, or if the distribution is empty, then an ArithmeticException is thrown. For example, {0.6, 0.4, 0.2, 0.8} -> {0.3, 0.2, 0.1, 0.4} -> {0.3, 0.5, 0.6, 1.0} The probabilities are retrieved and set using chooser. -
organizeDistribution
Same as organizeDistribution(objs, chooser, false); -
organizeDistribution
public static void organizeDistribution(Object[] objs, RandomChoiceChooserD chooser, boolean allowAllZeros) Normalizes the probabilities associated with an array of objects, then converts them into continuing sums. This prepares them for being usable in pickFromDistribution. If the probabilities are all 0, then selection is uniform, unless allowAllZeros is false, in which case an ArithmeticException is thrown. If any of them are negative, or if the distribution is empty, then an ArithmeticException is thrown. For example, {0.6, 0.4, 0.2, 0.8} -> {0.3, 0.2, 0.1, 0.4} -> {0.3, 0.5, 0.6, 1.0} The probabilities are retrieved and set using chooser. -
pickFromDistribution
public 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}. If probabilities.length invalid input: '<' CHECKBOUNDARY, then a linear search is used, else a binary search is used. -
pickFromDistribution
public 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}. If probabilities.length invalid input: '<' checkboundary, then a linear search is used, else a binary search is used. -
pickFromDistribution
public 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}. If probabilities.length invalid input: '<' CHECKBOUNDARY, then a linear search is used, else a binary search is used. -
pickFromDistribution
public 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}. If probabilities.length invalid input: '<' checkboundary, then a linear search is used, else a binary search is used. -
pickFromDistribution
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). The objects' probabilities are 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}. If probabilities.length invalid input: '<' CHECKBOUNDARY, then a linear search is used, else a binary search is used. -
pickFromDistribution
public static int pickFromDistribution(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). The objects' probabilities are 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}. If probabilities.length invalid input: '<' checkboundary, then a linear search is used, else a binary search is used. -
pickFromDistribution
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). The objects' probabilities are 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}. If probabilities.length invalid input: '<' CHECKBOUNDARY, then a linear search is used, else a binary search is used. -
pickFromDistribution
public static int pickFromDistribution(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). The objects' probabilities are 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}. If probabilities.length invalid input: '<' checkboundary, then a linear search is used, else a binary search is used.
-