From 1f0d6a4a3cd6039aad89e648bd8db4f71e3d9154 Mon Sep 17 00:00:00 2001 From: "ray.j.greenwell" Date: Wed, 13 Oct 2010 19:45:51 +0000 Subject: [PATCH] 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 --- src/main/java/com/samskivert/util/Randoms.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/samskivert/util/Randoms.java b/src/main/java/com/samskivert/util/Randoms.java index 22f2f649..7d394b9c 100644 --- a/src/main/java/com/samskivert/util/Randoms.java +++ b/src/main/java/com/samskivert/util/Randoms.java @@ -144,10 +144,10 @@ public class Randoms /** * Pick a random key from the specified mapping of weight values, or return - * ifEmpty if no mapping has a weight greater than 0. + * ifEmpty if no mapping has a weight greater than 0. * *

Implementation note: 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) {