diff --git a/src/java/com/threerings/util/RandomUtil.java b/src/java/com/threerings/util/RandomUtil.java index 68dab06fc..6fbe59956 100644 --- a/src/java/com/threerings/util/RandomUtil.java +++ b/src/java/com/threerings/util/RandomUtil.java @@ -76,7 +76,7 @@ public class RandomUtil * Pick a random index from the array, weighted by the value of the * corresponding array element. * - * @param weights an array of positive integers. + * @param weights an array of non-negative integers. * @return an index into the array, or -1 if the sum of the weights * is less than 1. * @@ -105,6 +105,46 @@ public class RandomUtil return 0; } + /** + * Pick a random index from the array, weighted by the value of the + * corresponding array element. + * + * @param weights an array of non-negative floats. + * @return an index into the array, or -1 if the sum of the weights + * is less than or equal to 0.0 or any individual element is negative. + * + * For example, passing in {0.2, 0.0, 0.6, 0.8} will return + *
| 0 | 1/8th of the time |
| 1 | never |
| 2 | 3/8th of the time |
| 3 | half of the time |