You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2019/08/07 19:34:11 UTC

[commons-rng] branch master updated: Change for loop to while loop.

This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-rng.git


The following commit(s) were added to refs/heads/master by this push:
     new 9b7a2d9  Change for loop to while loop.
9b7a2d9 is described below

commit 9b7a2d9fdbd08ba3be78bc50f5104daef0e1ecc5
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Wed Aug 7 20:34:06 2019 +0100

    Change for loop to while loop.
    
    The loop is not conditioned to stop on the counter incremented in the
    loop.
---
 .../distribution/MarsagliaTsangWangDiscreteSampler.java    | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/MarsagliaTsangWangDiscreteSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/MarsagliaTsangWangDiscreteSampler.java
index 97b9228..8c04309 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/MarsagliaTsangWangDiscreteSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/MarsagliaTsangWangDiscreteSampler.java
@@ -775,9 +775,9 @@ public final class MarsagliaTsangWangDiscreteSampler {
             // Recursive update of Poisson probability until the value is too small
             // p(x + 1) = p(x) * mean / (x + 1)
             double p = p0;
-            int i;
-            for (i = 1; p * DOUBLE_31 >= 1; i++) {
-                p *= mean / i;
+            int i = 1;
+            while (p * DOUBLE_31 >= 1) {
+                p *= mean / i++;
             }
 
             // Probabilities are 30-bit integers, assumed denominator 2^30
@@ -820,16 +820,16 @@ public final class MarsagliaTsangWangDiscreteSampler {
             // before the divisor i is large enough to start reducing the product (i.e. i > c).
             final double c = mean * Math.exp(-mean / mode);
             double p = 1.0;
-            int i;
-            for (i = 1; i <= mode; i++) {
+            for (int i = 1; i <= mode; i++) {
                 p *= c / i;
             }
             final double pMode = p;
 
             // Find the upper limit using recursive computation of the p-value.
             // Note this will exit when i overflows to negative so no check on the range
-            for (i = mode + 1; p * DOUBLE_31 >= 1; i++) {
-                p *= mean / i;
+            int i = mode + 1;
+            while (p * DOUBLE_31 >= 1) {
+                p *= mean / i++;
             }
             final int last = i - 2;