From f1e2670f72723678eb7cfdf913ef3c1a5b586b0d Mon Sep 17 00:00:00 2001 From: Phil Steitz Date: Sat, 31 Jan 2004 06:58:46 +0000 Subject: [PATCH] Added bound to simulation loop in nextPoisson(). git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141088 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/commons/math/random/RandomDataImpl.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/java/org/apache/commons/math/random/RandomDataImpl.java b/src/java/org/apache/commons/math/random/RandomDataImpl.java index 426313bad..7d989d6ee 100644 --- a/src/java/org/apache/commons/math/random/RandomDataImpl.java +++ b/src/java/org/apache/commons/math/random/RandomDataImpl.java @@ -105,7 +105,7 @@ import java.util.Collection; * (so secure sequences started with calls to reseedSecure(long) won't be * identical). * - * @version $Revision: 1.10 $ $Date: 2004/01/29 00:49:01 $ + * @version $Revision: 1.11 $ $Date: 2004/01/31 06:58:46 $ */ public class RandomDataImpl implements RandomData, Serializable { @@ -297,11 +297,15 @@ public class RandomDataImpl implements RandomData, Serializable { } /** + * Generates a random long value from the Poisson distribution with the given mean. + *

* Algorithm Description: * Uses simulation of a Poisson process using Uniform deviates, as * described * - * here + * here. + *

+ * The Poisson process (and hence value returned) is bounded by 1000 * mean. * @param mean mean of the Poisson distribution. * @return the random Poisson value. */ @@ -312,9 +316,10 @@ public class RandomDataImpl implements RandomData, Serializable { double p = Math.exp(-mean); long n = 0; double r = 1.0d; + double rnd = 1.0d; Random rand = getRan(); - while (true) { - double rnd = rand.nextDouble(); + while (n < 1000 * mean) { + rnd = rand.nextDouble(); r = r * rnd; if (r >= p) { n++; @@ -322,6 +327,7 @@ public class RandomDataImpl implements RandomData, Serializable { return n; } } + return n; } /**