Fixed ArrayUtils::pickRandom() when $rand=0.
authorAaron Schulz <aschulz@wikimedia.org>
Tue, 5 Mar 2013 23:18:47 +0000 (15:18 -0800)
committerAaron Schulz <aschulz@wikimedia.org>
Tue, 5 Mar 2013 23:18:47 +0000 (15:18 -0800)
Change-Id: Iba7db16a9ad08dd845fa1b4dd2bc02ffad8c5a32

includes/ArrayUtils.php

index 39d43e8..0b74f06 100644 (file)
@@ -58,7 +58,9 @@ class ArrayUtils {
                $sum = 0;
                foreach ( $weights as $i => $w ) {
                        $sum += $w;
-                       if ( $sum >= $rand ) {
+                       # Do not return keys if they have 0 weight.
+                       # Note that the "all 0 weight" case is handed above
+                       if ( $w > 0 && $sum >= $rand ) {
                                break;
                        }
                }