Avoid generating a random value for the first weighted entry.

git-svn-id: https://samskivert.googlecode.com/svn/trunk@2917 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
ray.j.greenwell
2010-10-13 19:45:51 +00:00
parent 3dadbe13b9
commit 1f0d6a4a3c
@@ -144,10 +144,10 @@ public class Randoms
/**
* Pick a random key from the specified mapping of weight values, or return
* <code>ifEmpty</code> if no mapping has a weight greater than 0.
* <code>ifEmpty</code> if no mapping has a weight greater than <code>0</code>.
*
* <p><b>Implementation note:</b> a random number is generated for every entry with a
* non-zero weight.
* non-zero weight after the first such entry.
*
* @throws IllegalArgumentException if any weight is less than 0.
*/
@@ -159,7 +159,7 @@ public class Randoms
double weight = entry.getValue().doubleValue();
if (weight > 0.0) {
total += weight;
if ((_r.nextDouble() * total) < weight) {
if ((total == weight) || ((_r.nextDouble() * total) < weight)) {
pick = entry.getKey();
}
} else if (weight < 0.0) {