You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by td...@apache.org on 2010/09/02 06:33:39 UTC
svn commit: r991806 [1/2] - in /mahout/trunk: core/
core/src/main/java/org/apache/mahout/common/
core/src/test/java/org/apache/mahout/classifier/sgd/
examples/src/main/java/org/apache/mahout/classifier/bayes/ math/
math/src/main/java/org/apache/mahout/...
Author: tdunning
Date: Thu Sep 2 04:33:38 2010
New Revision: 991806
URL: http://svn.apache.org/viewvc?rev=991806&view=rev
Log:
MAHOUT-495 - Moved RandomUtils and uncommons math dependency to math to allow refactoring of random stuff. Switched all distributions to use RandomUtils.
Added:
mahout/trunk/math/src/main/java/org/apache/mahout/common/
mahout/trunk/math/src/main/java/org/apache/mahout/common/RandomUtils.java
- copied, changed from r991805, mahout/trunk/core/src/main/java/org/apache/mahout/common/RandomUtils.java
mahout/trunk/math/src/main/java/org/apache/mahout/common/RandomWrapper.java
- copied, changed from r991805, mahout/trunk/core/src/main/java/org/apache/mahout/common/RandomWrapper.java
Removed:
mahout/trunk/core/src/main/java/org/apache/mahout/common/RandomUtils.java
mahout/trunk/core/src/main/java/org/apache/mahout/common/RandomWrapper.java
Modified:
mahout/trunk/core/pom.xml
mahout/trunk/core/src/test/java/org/apache/mahout/classifier/sgd/AdaptiveLogisticRegressionTest.java
mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/bayes/SplitBayesInput.java
mahout/trunk/math/pom.xml
mahout/trunk/math/src/main/java/org/apache/mahout/math/GenericPermuting.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/AbstractDistribution.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Beta.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Binomial.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/BreitWigner.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/BreitWignerMeanSquare.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/ChiSquare.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Empirical.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/EmpiricalWalker.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Exponential.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/ExponentialPower.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Gamma.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/HyperGeometric.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Hyperbolic.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Logarithmic.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/NegativeBinomial.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Normal.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Poisson.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/PoissonSlow.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/StudentT.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Uniform.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/VonMises.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Zeta.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/RandomSampler.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/RandomSamplingAssistant.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/WeightedRandomSampler.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/stat/quantile/KnownDoubleQuantileEstimator.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/stat/quantile/QuantileFinderFactory.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/stat/quantile/UnknownDoubleQuantileEstimator.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/DoubleFactory1D.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/DoubleFactory2D.java
mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/doublealgo/Statistic.java
mahout/trunk/math/src/test/java/org/apache/mahout/math/jet/random/ExponentialTest.java
mahout/trunk/math/src/test/java/org/apache/mahout/math/jet/random/GammaTest.java
mahout/trunk/math/src/test/java/org/apache/mahout/math/jet/random/NegativeBinomialTest.java
mahout/trunk/math/src/test/java/org/apache/mahout/math/jet/random/NormalTest.java
Modified: mahout/trunk/core/pom.xml
URL: http://svn.apache.org/viewvc/mahout/trunk/core/pom.xml?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/core/pom.xml (original)
+++ mahout/trunk/core/pom.xml Thu Sep 2 04:33:38 2010
@@ -211,12 +211,6 @@
</dependency>
<dependency>
- <groupId>org.uncommons.maths</groupId>
- <artifactId>uncommons-maths</artifactId>
- <version>1.2</version>
- </dependency>
-
- <dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.3.1</version>
Modified: mahout/trunk/core/src/test/java/org/apache/mahout/classifier/sgd/AdaptiveLogisticRegressionTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/classifier/sgd/AdaptiveLogisticRegressionTest.java?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/classifier/sgd/AdaptiveLogisticRegressionTest.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/classifier/sgd/AdaptiveLogisticRegressionTest.java Thu Sep 2 04:33:38 2010
@@ -25,19 +25,21 @@ import org.apache.mahout.math.jet.random
import org.junit.Assert;
import org.junit.Test;
+import java.util.Random;
+
public class AdaptiveLogisticRegressionTest {
@Test
public void testTrain() {
// we make up data for a simple model
- final MersenneTwister gen = new MersenneTwister(1);
- final Exponential exp = new Exponential(.5, gen);
+ final Random gen = RandomUtils.getRandom();
+ final Exponential exp = new Exponential(0.5, gen);
Vector beta = new DenseVector(200);
for (Vector.Element element : beta) {
- int sign = 1;
- if (gen.nextDouble() < 0.5) {
- sign = -1;
- }
+ int sign = 1;
+ if (gen.nextDouble() < 0.5) {
+ sign = -1;
+ }
element.set(sign * exp.nextDouble());
}
@@ -67,7 +69,7 @@ public class AdaptiveLogisticRegressionT
}
}
- private AdaptiveLogisticRegression.TrainingExample getExample(int i, MersenneTwister gen, Vector beta) {
+ private AdaptiveLogisticRegression.TrainingExample getExample(int i, Random gen, Vector beta) {
Vector data = new DenseVector(200);
for (Vector.Element element : data) {
@@ -85,9 +87,9 @@ public class AdaptiveLogisticRegressionT
@Test
public void copyLearnsAsExpected() {
RandomUtils.useTestSeed();
-
- final MersenneTwister gen = new MersenneTwister(1);
- final Exponential exp = new Exponential(.5, gen);
+
+ final Random gen = RandomUtils.getRandom();
+ final Exponential exp = new Exponential(0.5, gen);
Vector beta = new DenseVector(200);
for (Vector.Element element : beta) {
int sign = 1;
Modified: mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/bayes/SplitBayesInput.java
URL: http://svn.apache.org/viewvc/mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/bayes/SplitBayesInput.java?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/bayes/SplitBayesInput.java (original)
+++ mahout/trunk/examples/src/main/java/org/apache/mahout/classifier/bayes/SplitBayesInput.java Thu Sep 2 04:33:38 2010
@@ -39,6 +39,7 @@ import org.apache.commons.cli2.builder.G
import org.apache.commons.cli2.commandline.Parser;
import org.apache.mahout.common.CommandLineUtil;
import org.apache.mahout.common.IOUtils;
+import org.apache.mahout.common.RandomUtils;
import org.apache.mahout.common.commandline.DefaultOptionCreator;
import org.apache.mahout.math.jet.random.engine.MersenneTwister;
import org.apache.mahout.math.jet.random.sampling.RandomSampler;
@@ -302,7 +303,7 @@ public class SplitBayesInput {
log.info("{} test split size is {} based on random selection percentage {}",
new Object[] {inputFile.getName(), testSplitSize, testRandomSelectionPct});
long[] ridx = new long[testSplitSize];
- RandomSampler.sample(testSplitSize, lineCount-1, testSplitSize, 0, ridx, 0, new MersenneTwister(new Date()));
+ RandomSampler.sample(testSplitSize, lineCount-1, testSplitSize, 0, ridx, 0, RandomUtils.getRandom());
randomSel = new BitSet(lineCount);
for (long idx : ridx) {
randomSel.set((int) idx + 1);
Modified: mahout/trunk/math/pom.xml
URL: http://svn.apache.org/viewvc/mahout/trunk/math/pom.xml?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/math/pom.xml (original)
+++ mahout/trunk/math/pom.xml Thu Sep 2 04:33:38 2010
@@ -94,6 +94,12 @@
</dependency>
<dependency>
+ <groupId>org.uncommons.maths</groupId>
+ <artifactId>uncommons-maths</artifactId>
+ <version>1.2</version>
+ </dependency>
+
+ <dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>r03</version>
Copied: mahout/trunk/math/src/main/java/org/apache/mahout/common/RandomUtils.java (from r991805, mahout/trunk/core/src/main/java/org/apache/mahout/common/RandomUtils.java)
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/common/RandomUtils.java?p2=mahout/trunk/math/src/main/java/org/apache/mahout/common/RandomUtils.java&p1=mahout/trunk/core/src/main/java/org/apache/mahout/common/RandomUtils.java&r1=991805&r2=991806&rev=991806&view=diff
==============================================================================
(empty)
Copied: mahout/trunk/math/src/main/java/org/apache/mahout/common/RandomWrapper.java (from r991805, mahout/trunk/core/src/main/java/org/apache/mahout/common/RandomWrapper.java)
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/common/RandomWrapper.java?p2=mahout/trunk/math/src/main/java/org/apache/mahout/common/RandomWrapper.java&p1=mahout/trunk/core/src/main/java/org/apache/mahout/common/RandomWrapper.java&r1=991805&r2=991806&rev=991806&view=diff
==============================================================================
(empty)
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/GenericPermuting.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/GenericPermuting.java?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/GenericPermuting.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/GenericPermuting.java Thu Sep 2 04:33:38 2010
@@ -8,6 +8,7 @@ It is provided "as is" without expressed
*/
package org.apache.mahout.math;
+import org.apache.mahout.common.RandomUtils;
import org.apache.mahout.math.jet.random.Uniform;
import org.apache.mahout.math.jet.random.engine.MersenneTwister;
@@ -156,7 +157,7 @@ public class GenericPermuting {
for (int i = N; --i >= 0;) {
permutation[i] = i;
}
- Uniform gen = new Uniform(new MersenneTwister((int) p));
+ Uniform gen = new Uniform(RandomUtils.getRandom());
for (int i = 0; i < N - 1; i++) {
int random = gen.nextIntFromTo(i, N - 1);
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/AbstractDistribution.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/AbstractDistribution.java?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/AbstractDistribution.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/AbstractDistribution.java Thu Sep 2 04:33:38 2010
@@ -28,10 +28,11 @@ package org.apache.mahout.math.jet.rando
import org.apache.mahout.math.PersistentObject;
import org.apache.mahout.math.function.UnaryFunction;
import org.apache.mahout.math.function.IntFunction;
-import org.apache.mahout.math.jet.random.engine.RandomEngine;
+
+import java.util.Random;
public abstract class AbstractDistribution extends PersistentObject implements UnaryFunction, IntFunction {
- protected RandomEngine randomGenerator;
+ protected Random randomGenerator;
/** Makes this class non instantiable, but still let's others inherit from it. */
protected AbstractDistribution() {
@@ -54,46 +55,33 @@ public abstract class AbstractDistributi
}
/**
- * Returns a deep copy of the receiver; the copy will produce identical sequences. After this call has returned, the
- * copy and the receiver have equal but separate state.
- *
- * @return a copy of the receiver.
- */
- @Override
- public Object clone() {
- AbstractDistribution copy = (AbstractDistribution) super.clone();
- if (this.randomGenerator != null) {
- copy.randomGenerator = (RandomEngine) this.randomGenerator.clone();
- }
- return copy;
- }
-
- /** Returns the used uniform random number generator; */
- protected RandomEngine getRandomGenerator() {
- return randomGenerator;
- }
-
- /**
- * Constructs and returns a new uniform random number generation engine seeded with the current time. Currently this
- * is {@link org.apache.mahout.math.jet.random.engine.MersenneTwister}.
+ * Returns a random number from the distribution.
+ * @return A new sample from this distribution.
*/
- public static RandomEngine makeDefaultGenerator() {
- return org.apache.mahout.math.jet.random.engine.RandomEngine.makeDefault();
- }
-
- /** Returns a random number from the distribution. */
public abstract double nextDouble();
/**
- * Returns a random number from the distribution; returns <tt>(int) Math.round(nextDouble())</tt>. Override this
+ * @return
+ * A random number from the distribution; returns <tt>(int) Math.round(nextDouble())</tt>. Override this
* method if necessary.
*/
public int nextInt() {
return (int) Math.round(nextDouble());
}
-
- /** Sets the uniform random generator internally used. */
- protected void setRandomGenerator(RandomEngine randomGenerator) {
+
+ /**
+ * Returns the used uniform random number generator;
+ * @return The current PRNG underlying this distribution.
+ */
+ public Random getRandomGenerator() {
+ return randomGenerator;
+ }
+
+ /**
+ * Sets the uniform random generator internally used.
+ * @param randomGenerator the new PRNG
+ */
+ public void setRandomGenerator(Random randomGenerator) {
this.randomGenerator = randomGenerator;
}
}
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Beta.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Beta.java?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Beta.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Beta.java Thu Sep 2 04:33:38 2010
@@ -8,9 +8,12 @@ It is provided "as is" without expressed
*/
package org.apache.mahout.math.jet.random;
+import org.apache.mahout.common.RandomUtils;
import org.apache.mahout.math.jet.random.engine.RandomEngine;
import org.apache.mahout.math.jet.stat.Probability;
+import java.util.Random;
+
/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
@Deprecated
public class Beta extends AbstractContinousDistribution {
@@ -60,12 +63,8 @@ public class Beta extends AbstractContin
private double p3;
private double p4;
-
- // The uniform random number generated shared by all <b>static</b> methods.
- private static final Beta shared = new Beta(10.0, 10.0, makeDefaultGenerator());
-
/** Constructs a Beta distribution. */
- public Beta(double alpha, double beta, RandomEngine randomGenerator) {
+ public Beta(double alpha, double beta, Random randomGenerator) {
setRandomGenerator(randomGenerator);
setState(alpha, beta);
}
@@ -73,7 +72,7 @@ public class Beta extends AbstractContin
/**
*
*/
- protected double b00(double a, double b, RandomEngine randomGenerator) {
+ protected double b00(double a, double b, Random randomGenerator) {
if (a != aLast || b != bLast) {
aLast = a;
@@ -95,11 +94,11 @@ public class Beta extends AbstractContin
double Z;
double V;
double U;
- if ((U = randomGenerator.raw() * p2) <= p1) { // X < t
+ if ((U = randomGenerator.nextDouble() * p2) <= p1) { // X < t
Z = Math.exp(Math.log(U / p1) / a);
X = t * Z;
// squeeze accept: L(x) = 1 + (1 - b)x
- if ((V = randomGenerator.raw() * fb) <= 1.0 - bMinus1 * X) {
+ if ((V = randomGenerator.nextDouble() * fb) <= 1.0 - bMinus1 * X) {
break;
}
// squeeze reject: U(x) = 1 + ((1 - t)^(b-1) - 1)/t * x
@@ -113,7 +112,7 @@ public class Beta extends AbstractContin
Z = Math.exp(Math.log((U - p1) / (p2 - p1)) / b);
X = 1.0 - (1.0 - t) * Z;
// squeeze accept: L(x) = 1 + (1 - a)(1 - x)
- if ((V = randomGenerator.raw() * fa) <= 1.0 - aMinus1 * (1.0 - X)) {
+ if ((V = randomGenerator.nextDouble() * fa) <= 1.0 - aMinus1 * (1.0 - X)) {
break;
}
// squeeze reject: U(x) = 1 + (t^(a-1) - 1)/(1 - t) * (1 - x)
@@ -131,7 +130,7 @@ public class Beta extends AbstractContin
/**
*
*/
- protected double b01(double a, double b, RandomEngine randomGenerator) {
+ protected double b01(double a, double b, Random randomGenerator) {
if (a != aLast || b != bLast) {
aLast = a;
@@ -161,11 +160,11 @@ public class Beta extends AbstractContin
double Z;
double V;
double U;
- if ((U = randomGenerator.raw() * p2) <= p1) { // X < t
+ if ((U = randomGenerator.nextDouble() * p2) <= p1) { // X < t
Z = Math.exp(Math.log(U / p1) / a);
X = t * Z;
// squeeze accept: L(x) = 1 + m1*x, ml = -m1
- if ((V = randomGenerator.raw()) <= 1.0 - ml * X) {
+ if ((V = randomGenerator.nextDouble()) <= 1.0 - ml * X) {
break;
}
// squeeze reject: U(x) = 1 + m2*x, mu = -m2 * t
@@ -179,7 +178,8 @@ public class Beta extends AbstractContin
Z = Math.exp(Math.log((U - p1) / (p2 - p1)) / b);
X = 1.0 - (1.0 - t) * Z;
// squeeze accept: L(x) = 1 + (1 - a)(1 - x)
- if ((V = randomGenerator.raw() * fa) <= 1.0 - aMinus1 * (1.0 - X)) {
+ V = randomGenerator.nextDouble() * fa;
+ if ((V) <= 1.0 - aMinus1 * (1.0 - X)) {
break;
}
// squeeze reject: U(x) = 1 + (t^(a-1) - 1)/(1 - t) * (1 - x)
@@ -197,7 +197,7 @@ public class Beta extends AbstractContin
/**
*
*/
- protected double b1prs(double p, double q, RandomEngine randomGenerator) {
+ protected double b1prs(double p, double q, Random randomGenerator) {
if (p != pLast || q != qLast) {
pLast = p;
@@ -261,7 +261,7 @@ public class Beta extends AbstractContin
double W;
double V;
double U;
- if ((U = randomGenerator.raw() * p4) <= p1) {
+ if ((U = randomGenerator.nextDouble() * p4) <= p1) {
// immediate accept: x2 < X < m, - f(x2) < W < 0
if ((W = U / d1 - f2) <= 0.0) {
return (m - U / f2);
@@ -271,7 +271,7 @@ public class Beta extends AbstractContin
return (x2 - W / f1 * d1);
}
// candidates for acceptance-rejection-test
- V = d1 * (U = randomGenerator.raw());
+ V = d1 * (U = randomGenerator.nextDouble());
X = x2 - V;
Y = x2 + V;
// squeeze accept: L(x) = f(x2) (x - z2) / (x2 - z2)
@@ -299,7 +299,7 @@ public class Beta extends AbstractContin
return (x4 + W / f5 * d);
}
// candidates for acceptance-rejection-test
- V = d * (U = randomGenerator.raw());
+ V = d * (U = randomGenerator.nextDouble());
X = x4 + V;
Y = x4 - V;
// squeeze accept: L(x) = f(x4) (z4 - x) / (z4 - x4)
@@ -321,7 +321,7 @@ public class Beta extends AbstractContin
if ((X = x1 + ll * Y) <= 0.0) {
continue;
} // X > 0!!
- W = randomGenerator.raw() * U;
+ W = randomGenerator.nextDouble() * U;
// squeeze accept: L(x) = f(x1) (x - z1) / (x1 - z1)
// z1 = x1 - ll, W <= 1 + (X - x1)/ll
if (W <= 1.0 + Y) {
@@ -333,7 +333,7 @@ public class Beta extends AbstractContin
if ((X = x5 - lr * Y) >= 1.0) {
continue;
} // X < 1!!
- W = randomGenerator.raw() * U;
+ W = randomGenerator.nextDouble() * U;
// squeeze accept: L(x) = f(x5) (z5 - x) / (z5 - x5)
// z5 = x5 + lr, W <= 1 + (x5 - X)/lr
if (W <= 1.0 + Y) {
@@ -414,7 +414,7 @@ public class Beta extends AbstractContin
if (beta < 1.0) {
return (1.0 - b01(beta, alpha, randomGenerator));
}
- return (Math.exp(Math.log(randomGenerator.raw()) / alpha));
+ return (Math.exp(Math.log(1 - randomGenerator.nextDouble()) / alpha));
}
if (alpha < 1.0) {
@@ -424,10 +424,10 @@ public class Beta extends AbstractContin
if (beta < 1.0) {
return (b00(alpha, beta, randomGenerator));
}
- return (Math.exp(Math.log(randomGenerator.raw()) / alpha));
+ return (Math.exp(Math.log(randomGenerator.nextDouble()) / alpha));
}
- return beta == 1.0 ? randomGenerator.raw() : 1.0 - Math.exp(Math.log(randomGenerator.raw()) / beta);
+ return beta == 1.0 ? randomGenerator.nextDouble() : 1.0 - Math.exp(Math.log(randomGenerator.nextDouble()) / beta);
}
/** Returns the cumulative distribution function. */
@@ -445,13 +445,6 @@ public class Beta extends AbstractContin
this.PDF_CONST = Fun.logGamma(alpha + beta) - Fun.logGamma(alpha) - Fun.logGamma(beta);
}
- /** Returns a random number from the distribution. */
- public static double staticNextDouble(double alpha, double beta) {
- synchronized (shared) {
- return shared.nextDouble(alpha, beta);
- }
- }
-
/** Returns a String representation of the receiver. */
public String toString() {
return this.getClass().getName() + '(' + alpha + ',' + beta + ')';
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Binomial.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Binomial.java?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Binomial.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Binomial.java Thu Sep 2 04:33:38 2010
@@ -8,10 +8,12 @@ It is provided "as is" without expressed
*/
package org.apache.mahout.math.jet.random;
+import org.apache.mahout.common.RandomUtils;
import org.apache.mahout.math.jet.math.Arithmetic;
-import org.apache.mahout.math.jet.random.engine.RandomEngine;
import org.apache.mahout.math.jet.stat.Probability;
+import java.util.Random;
+
/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
@Deprecated
public class Binomial extends AbstractDiscreteDistribution {
@@ -54,7 +56,7 @@ public class Binomial extends AbstractDi
private double logN;
// The uniform random number generated shared by all <b>static</b> methods.
- private static final Binomial shared = new Binomial(1, 0.5, makeDefaultGenerator());
+ private static final Binomial shared = new Binomial(1, 0.5, RandomUtils.getRandom());
/**
* Constructs a binomial distribution. Example: n=1, p=0.5.
*
@@ -63,7 +65,7 @@ public class Binomial extends AbstractDi
* @param randomGenerator a uniform random number generator.
* @throws IllegalArgumentException if <tt>n*Math.min(p,1-p) <= 0.0</tt>
*/
- public Binomial(int n, double p, RandomEngine randomGenerator) {
+ public Binomial(int n, double p, Random randomGenerator) {
setRandomGenerator(randomGenerator);
setNandP(n, p);
}
@@ -158,11 +160,11 @@ public class Binomial extends AbstractDi
K = 0;
double pk = p0;
- U = randomGenerator.raw();
+ U = randomGenerator.nextDouble();
while (U > pk) {
++K;
if (K > b) {
- U = randomGenerator.raw();
+ U = randomGenerator.nextDouble();
K = 0;
pk = p0;
} else {
@@ -174,8 +176,8 @@ public class Binomial extends AbstractDi
}
while (true) {
- double V = randomGenerator.raw();
- if ((U = randomGenerator.raw() * p4) <= p1) { // triangular region
+ double V = randomGenerator.nextDouble();
+ if ((U = randomGenerator.nextDouble() * p4) <= p1) { // triangular region
K = (int) (xm - U + p1 * V);
return (p > 0.5) ? (n - K) : K; // immediate accept
}
@@ -254,12 +256,6 @@ public class Binomial extends AbstractDi
return (p > 0.5) ? (n - K) : K;
}
- /** Returns a random number from the distribution. */
- @Override
- public int nextInt() {
- return generateBinomial(n, p);
- }
-
/**
* Returns a random number from the distribution with the given parameters n and p; bypasses the internal state.
*
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/BreitWigner.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/BreitWigner.java?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/BreitWigner.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/BreitWigner.java Thu Sep 2 04:33:38 2010
@@ -8,8 +8,11 @@ It is provided "as is" without expressed
*/
package org.apache.mahout.math.jet.random;
+import org.apache.mahout.common.RandomUtils;
import org.apache.mahout.math.jet.random.engine.RandomEngine;
+import java.util.Random;
+
/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
@Deprecated
public class BreitWigner extends AbstractContinousDistribution {
@@ -19,14 +22,14 @@ public class BreitWigner extends Abstrac
private double cut;
// The uniform random number generated shared by all <b>static</b> methods.
- private static final BreitWigner shared = new BreitWigner(1.0, 0.2, 1.0, makeDefaultGenerator());
+ private static final BreitWigner shared = new BreitWigner(1.0, 0.2, 1.0, RandomUtils.getRandom());
/**
* Constructs a BreitWigner distribution.
*
* @param cut </tt>cut==Double.NEGATIVE_INFINITY</tt> indicates "don't cut".
*/
- public BreitWigner(double mean, double gamma, double cut, RandomEngine randomGenerator) {
+ public BreitWigner(double mean, double gamma, double cut, Random randomGenerator) {
setRandomGenerator(randomGenerator);
setState(mean, gamma, cut);
}
@@ -50,12 +53,12 @@ public class BreitWigner extends Abstrac
double displ;
double rval;
if (cut == Double.NEGATIVE_INFINITY) { // don't cut
- rval = 2.0 * randomGenerator.raw() - 1.0;
+ rval = 2.0 * randomGenerator.nextDouble() - 1.0;
displ = 0.5 * gamma * Math.tan(rval * (Math.PI / 2.0));
return mean + displ;
} else {
double val = Math.atan(2.0 * cut / gamma);
- rval = 2.0 * randomGenerator.raw() - 1.0;
+ rval = 2.0 * randomGenerator.nextDouble() - 1.0;
displ = 0.5 * gamma * Math.tan(rval * val);
return mean + displ;
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/BreitWignerMeanSquare.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/BreitWignerMeanSquare.java?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/BreitWignerMeanSquare.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/BreitWignerMeanSquare.java Thu Sep 2 04:33:38 2010
@@ -8,8 +8,11 @@ It is provided "as is" without expressed
*/
package org.apache.mahout.math.jet.random;
+import org.apache.mahout.common.RandomUtils;
import org.apache.mahout.math.jet.random.engine.RandomEngine;
+import java.util.Random;
+
/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
@Deprecated
public class BreitWignerMeanSquare extends BreitWigner {
@@ -17,14 +20,14 @@ public class BreitWignerMeanSquare exten
private Uniform uniform; // helper
// The uniform random number generated shared by all <b>static</b> methods.
- private static final BreitWigner shared = new BreitWignerMeanSquare(1.0, 0.2, 1.0, makeDefaultGenerator());
+ private static final BreitWigner shared = new BreitWignerMeanSquare(1.0, 0.2, 1.0, RandomUtils.getRandom());
/**
* Constructs a mean-squared BreitWigner distribution.
*
* @param cut </tt>cut==Double.NEGATIVE_INFINITY</tt> indicates "don't cut".
*/
- public BreitWignerMeanSquare(double mean, double gamma, double cut, RandomEngine randomGenerator) {
+ public BreitWignerMeanSquare(double mean, double gamma, double cut, Random randomGenerator) {
super(mean, gamma, cut, randomGenerator);
this.uniform = new Uniform(randomGenerator);
}
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/ChiSquare.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/ChiSquare.java?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/ChiSquare.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/ChiSquare.java Thu Sep 2 04:33:38 2010
@@ -8,9 +8,12 @@ It is provided "as is" without expressed
*/
package org.apache.mahout.math.jet.random;
+import org.apache.mahout.common.RandomUtils;
import org.apache.mahout.math.jet.random.engine.RandomEngine;
import org.apache.mahout.math.jet.stat.Probability;
+import java.util.Random;
+
/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
@Deprecated
public class ChiSquare extends AbstractContinousDistribution {
@@ -24,7 +27,7 @@ public class ChiSquare extends AbstractC
private double vd;
// The uniform random number generated shared by all <b>static</b> methods.
- private static final ChiSquare shared = new ChiSquare(1.0, makeDefaultGenerator());
+ private static final ChiSquare shared = new ChiSquare(1.0, RandomUtils.getRandom());
/**
* Constructs a ChiSquare distribution. Example: freedom=1.0.
@@ -32,7 +35,7 @@ public class ChiSquare extends AbstractC
* @param freedom degrees of freedom.
* @throws IllegalArgumentException if <tt>freedom < 1.0</tt>.
*/
- public ChiSquare(double freedom, RandomEngine randomGenerator) {
+ public ChiSquare(double freedom, Random randomGenerator) {
setRandomGenerator(randomGenerator);
setState(freedom);
}
@@ -81,8 +84,8 @@ public class ChiSquare extends AbstractC
if (freedom == 1.0) {
while (true) {
- u = randomGenerator.raw();
- v = randomGenerator.raw() * 0.857763884960707;
+ u = randomGenerator.nextDouble();
+ v = randomGenerator.nextDouble() * 0.857763884960707;
z = v / u;
if (z < 0) {
continue;
@@ -112,8 +115,8 @@ public class ChiSquare extends AbstractC
freedom_in = freedom;
}
while (true) {
- u = randomGenerator.raw();
- v = randomGenerator.raw() * vd + vm;
+ u = randomGenerator.nextDouble();
+ v = randomGenerator.nextDouble() * vd + vm;
z = v / u;
if (z < -b) {
continue;
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Empirical.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Empirical.java?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Empirical.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Empirical.java Thu Sep 2 04:33:38 2010
@@ -10,6 +10,8 @@ package org.apache.mahout.math.jet.rando
import org.apache.mahout.math.jet.random.engine.RandomEngine;
+import java.util.Random;
+
/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
@Deprecated
public class Empirical extends AbstractContinousDistribution {
@@ -31,7 +33,7 @@ public class Empirical extends AbstractC
* @param randomGenerator a uniform random number generator.
* @throws IllegalArgumentException if at least one of the three conditions above is violated.
*/
- public Empirical(double[] pdf, int interpolationType, RandomEngine randomGenerator) {
+ public Empirical(double[] pdf, int interpolationType, Random randomGenerator) {
setRandomGenerator(randomGenerator);
setState(pdf, interpolationType);
}
@@ -65,7 +67,7 @@ public class Empirical extends AbstractC
/** Returns a random number from the distribution. */
@Override
public double nextDouble() {
- double rand = randomGenerator.raw();
+ double rand = randomGenerator.nextDouble();
if (this.cdf == null) {
return rand;
} // Non-existing pdf
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/EmpiricalWalker.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/EmpiricalWalker.java?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/EmpiricalWalker.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/EmpiricalWalker.java Thu Sep 2 04:33:38 2010
@@ -10,6 +10,8 @@ package org.apache.mahout.math.jet.rando
import org.apache.mahout.math.jet.random.engine.RandomEngine;
+import java.util.Random;
+
/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
@Deprecated
public class EmpiricalWalker extends AbstractDiscreteDistribution {
@@ -136,7 +138,7 @@ public class EmpiricalWalker extends Abs
* @param randomGenerator a uniform random number generator.
* @throws IllegalArgumentException if at least one of the three conditions above is violated.
*/
- public EmpiricalWalker(double[] pdf, int interpolationType, RandomEngine randomGenerator) {
+ public EmpiricalWalker(double[] pdf, int interpolationType, Random randomGenerator) {
setRandomGenerator(randomGenerator);
setState(pdf, interpolationType);
setState2(pdf);
@@ -174,10 +176,10 @@ public class EmpiricalWalker extends Abs
return copy;
}
- /** Returns a random integer <tt>k</tt> with probability <tt>pdf(k)</tt>. */
+ /** Returns a random integer <tt>k</tt> with probability <tt>pdf(k)</tt>. */
@Override
public int nextInt() {
- double u = this.randomGenerator.raw();
+ double u = this.randomGenerator.nextDouble();
u *= this.K;
int c = (int) u;
u -= c;
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Exponential.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Exponential.java?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Exponential.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Exponential.java Thu Sep 2 04:33:38 2010
@@ -11,6 +11,7 @@ package org.apache.mahout.math.jet.rando
import org.apache.mahout.math.jet.random.engine.RandomEngine;
import java.util.Locale;
+import java.util.Random;
public class Exponential extends AbstractContinousDistribution {
// rate parameter for the distribution. Mean is 1/lambda.
@@ -23,7 +24,7 @@ public class Exponential extends Abstrac
* @param lambda The rate parameter of the distribution.
* @param randomGenerator The PRNG that is used to generate values.
*/
- public Exponential(double lambda, RandomEngine randomGenerator) {
+ public Exponential(double lambda, Random randomGenerator) {
setRandomGenerator(randomGenerator);
setState(lambda);
}
@@ -46,7 +47,7 @@ public class Exponential extends Abstrac
*/
@Override
public double nextDouble() {
- return -Math.log(1 - randomGenerator.raw()) / lambda;
+ return -Math.log(1 - randomGenerator.nextDouble()) / lambda;
}
/**
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/ExponentialPower.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/ExponentialPower.java?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/ExponentialPower.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/ExponentialPower.java Thu Sep 2 04:33:38 2010
@@ -8,8 +8,11 @@ It is provided "as is" without expressed
*/
package org.apache.mahout.math.jet.random;
+import org.apache.mahout.common.RandomUtils;
import org.apache.mahout.math.jet.random.engine.RandomEngine;
+import java.util.Random;
+
/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
@Deprecated
public class ExponentialPower extends AbstractContinousDistribution {
@@ -22,14 +25,14 @@ public class ExponentialPower extends Ab
private double tau_set = -1.0;
// The uniform random number generated shared by all <b>static</b> methods.
- private static final ExponentialPower shared = new ExponentialPower(1.0, makeDefaultGenerator());
+ private static final ExponentialPower shared = new ExponentialPower(1.0, RandomUtils.getRandom());
/**
* Constructs an Exponential Power distribution. Example: tau=1.0.
*
* @throws IllegalArgumentException if <tt>tau < 1.0</tt>.
*/
- public ExponentialPower(double tau, RandomEngine randomGenerator) {
+ public ExponentialPower(double tau, Random randomGenerator) {
setRandomGenerator(randomGenerator);
setState(tau);
}
@@ -59,10 +62,10 @@ public class ExponentialPower extends Ab
double v;
double u;
do {
- u = randomGenerator.raw(); // U(0/1)
+ u = randomGenerator.nextDouble(); // U(0/1)
u = (2.0 * u) - 1.0; // U(-1.0/1.0)
double u1 = Math.abs(u);
- v = randomGenerator.raw(); // U(0/1)
+ v = randomGenerator.nextDouble(); // U(0/1)
if (u1 <= sm1) { // Uniform hat-function for x <= (1-1/tau)
x = u1;
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Gamma.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Gamma.java?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Gamma.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Gamma.java Thu Sep 2 04:33:38 2010
@@ -28,6 +28,8 @@ package org.apache.mahout.math.jet.rando
import org.apache.mahout.math.jet.random.engine.RandomEngine;
import org.apache.mahout.math.jet.stat.Probability;
+import java.util.Random;
+
public class Gamma extends AbstractContinousDistribution {
// shape
private final double alpha;
@@ -43,7 +45,7 @@ public class Gamma extends AbstractConti
* @param randomGenerator The random number generator that generates bits for us.
* @throws IllegalArgumentException if <tt>alpha <= 0.0 || alpha <= 0.0</tt>.
*/
- public Gamma(double alpha, double rate, RandomEngine randomGenerator) {
+ public Gamma(double alpha, double rate, Random randomGenerator) {
this.alpha = alpha;
this.rate = rate;
setRandomGenerator(randomGenerator);
@@ -101,15 +103,15 @@ public class Gamma extends AbstractConti
if (alpha < 1.0) { // CASE A: Acceptance rejection algorithm gs
b = 1.0 + 0.36788794412 * alpha; // Step 1
while (true) {
- double p = b * randomGenerator.raw();
+ double p = b * randomGenerator.nextDouble();
if (p <= 1.0) { // Step 2. Case gds <= 1
gds = Math.exp(Math.log(p) / alpha);
- if (Math.log(randomGenerator.raw()) <= -gds) {
+ if (Math.log(randomGenerator.nextDouble()) <= -gds) {
return (gds / rate);
}
} else { // Step 3. Case gds > 1
gds = -Math.log((b - p) / alpha);
- if (Math.log(randomGenerator.raw()) <= ((alpha - 1.0) * Math.log(gds))) {
+ if (Math.log(randomGenerator.nextDouble()) <= ((alpha - 1.0) * Math.log(gds))) {
return (gds / rate);
}
}
@@ -127,8 +129,8 @@ public class Gamma extends AbstractConti
double v12;
double v1;
do {
- v1 = 2.0 * randomGenerator.raw() - 1.0;
- double v2 = 2.0 * randomGenerator.raw() - 1.0;
+ v1 = 2.0 * randomGenerator.nextDouble() - 1.0;
+ double v2 = 2.0 * randomGenerator.nextDouble() - 1.0;
v12 = v1 * v1 + v2 * v2;
} while (v12 > 1.0);
double t = v1 * Math.sqrt(-2.0 * Math.log(v12) / v12);
@@ -138,7 +140,7 @@ public class Gamma extends AbstractConti
return (gds / rate);
} // Immediate acceptance
- double u = randomGenerator.raw();
+ double u = randomGenerator.nextDouble();
if (d * u <= t * t * t) {
return (gds / rate);
} // Squeeze acceptance
@@ -210,8 +212,8 @@ public class Gamma extends AbstractConti
double sign_u;
double e;
do {
- e = -Math.log(randomGenerator.raw());
- u = randomGenerator.raw();
+ e = -Math.log(randomGenerator.nextDouble());
+ u = randomGenerator.nextDouble();
u = u + u - 1.0;
sign_u = (u > 0) ? 1.0 : -1.0;
t = b + (e * si) * sign_u;
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/HyperGeometric.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/HyperGeometric.java?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/HyperGeometric.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/HyperGeometric.java Thu Sep 2 04:33:38 2010
@@ -8,9 +8,12 @@ It is provided "as is" without expressed
*/
package org.apache.mahout.math.jet.random;
+import org.apache.mahout.common.RandomUtils;
import org.apache.mahout.math.jet.math.Arithmetic;
import org.apache.mahout.math.jet.random.engine.RandomEngine;
+import java.util.Random;
+
/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
@Deprecated
public class HyperGeometric extends AbstractDiscreteDistribution {
@@ -60,10 +63,10 @@ public class HyperGeometric extends Abst
// The uniform random number generated shared by all <b>static</b> methods.
- private static final HyperGeometric shared = new HyperGeometric(1, 1, 1, makeDefaultGenerator());
+ private static final HyperGeometric shared = new HyperGeometric(1, 1, 1, RandomUtils.getRandom());
/** Constructs a HyperGeometric distribution. */
- public HyperGeometric(int N, int s, int n, RandomEngine randomGenerator) {
+ public HyperGeometric(int N, int s, int n, Random randomGenerator) {
setRandomGenerator(randomGenerator);
setState(N, s, n);
}
@@ -74,7 +77,7 @@ public class HyperGeometric extends Abst
}
/** Returns a random number from the distribution. */
- protected int hmdu(int N, int M, int n, RandomEngine randomGenerator) {
+ protected int hmdu(int N, int M, int n, Random randomGenerator) {
if (N != N_last || M != M_last || n != n_last) { // set-up */
N_last = N;
@@ -108,7 +111,7 @@ public class HyperGeometric extends Abst
while (true) {
double U;
- if ((U = randomGenerator.raw() - fm) <= 0.0) {
+ if ((U = randomGenerator.nextDouble() - fm) <= 0.0) {
return (m);
}
double d;
@@ -141,7 +144,7 @@ public class HyperGeometric extends Abst
}
/** Returns a random number from the distribution. */
- protected int hprs(int N, int M, int n, RandomEngine randomGenerator) {
+ protected int hprs(int N, int M, int n, Random randomGenerator) {
double U; /* (X, Y) <-> (V, W) */
if (N != N_last || M != M_last || n != n_last) { /* set-up */
@@ -214,7 +217,7 @@ public class HyperGeometric extends Abst
int V;
int X;
int Dk;
- if ((U = randomGenerator.raw() * p6) < p2) { // centre left
+ if ((U = randomGenerator.nextDouble() * p6) < p2) { // centre left
// immediate acceptance region R2 = [k2, m) *[0, f2), X = k2, ... m -1
if ((W = U - p1) < 0.0) {
@@ -227,7 +230,7 @@ public class HyperGeometric extends Abst
// computation of candidate X < k2, and its counterpart V > k2
// either squeeze-acceptance of X or acceptance-rejection of V
- Dk = (int) (dl * randomGenerator.raw()) + 1;
+ Dk = (int) (dl * randomGenerator.nextDouble()) + 1;
if (Y <= f2 - Dk * (f2 - f2 / r2)) { // quick accept of
return (k2 - Dk); // X = k2 - Dk
}
@@ -254,7 +257,7 @@ public class HyperGeometric extends Abst
// computation of candidate X > k4, and its counterpart V < k4
// either squeeze-acceptance of X or acceptance-rejection of V
- Dk = (int) (dr * randomGenerator.raw()) + 1;
+ Dk = (int) (dr * randomGenerator.nextDouble()) + 1;
if (Y <= f4 - Dk * (f4 - f4 * r4)) { // quick accept of
return (k4 + Dk); // X = k4 + Dk
}
@@ -269,7 +272,7 @@ public class HyperGeometric extends Abst
}
X = k4 + Dk;
} else {
- Y = randomGenerator.raw();
+ Y = randomGenerator.nextDouble();
if (U < p5) { // expon. tail left
Dk = (int) (1.0 - Math.log(Y) / ll);
if ((X = k1 - Dk) < 0) {
@@ -302,7 +305,11 @@ public class HyperGeometric extends Abst
}
}
- /** Returns a random number from the distribution. */
+ /**
+ * Returns a random number from the distribution.
+ *
+ * @return An integer sample from this hyper-geometric distribution.
+ */
@Override
public int nextInt() {
return nextInt(this.my_N, this.my_s, this.my_n, this.randomGenerator);
@@ -314,7 +321,7 @@ public class HyperGeometric extends Abst
}
/** Returns a random number from the distribution; bypasses the internal state. */
- protected int nextInt(int N, int M, int n, RandomEngine randomGenerator) {
+ protected int nextInt(int N, int M, int n, Random randomGenerator) {
/******************************************************************
* *
* Hypergeometric Distribution - Patchwork Rejection/Inversion *
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Hyperbolic.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Hyperbolic.java?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Hyperbolic.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Hyperbolic.java Thu Sep 2 04:33:38 2010
@@ -8,8 +8,11 @@ It is provided "as is" without expressed
*/
package org.apache.mahout.math.jet.random;
+import org.apache.mahout.common.RandomUtils;
import org.apache.mahout.math.jet.random.engine.RandomEngine;
+import java.util.Random;
+
/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
@Deprecated
public class Hyperbolic extends AbstractContinousDistribution {
@@ -32,10 +35,10 @@ public class Hyperbolic extends Abstract
// The uniform random number generated shared by all <b>static</b> methods.
- private static final Hyperbolic shared = new Hyperbolic(10.0, 10.0, makeDefaultGenerator());
+ private static final Hyperbolic shared = new Hyperbolic(10.0, 10.0, RandomUtils.getRandom());
/** Constructs a Beta distribution. */
- public Hyperbolic(double alpha, double beta, RandomEngine randomGenerator) {
+ public Hyperbolic(double alpha, double beta, Random randomGenerator) {
setRandomGenerator(randomGenerator);
setState(alpha, beta);
}
@@ -96,8 +99,8 @@ public class Hyperbolic extends Abstract
// GENERATOR
double x;
while (true) {
- double u = randomGenerator.raw();
- double v = randomGenerator.raw();
+ double u = randomGenerator.nextDouble();
+ double v = randomGenerator.nextDouble();
if (u <= pm) { // Rejection with a uniform majorizing function
// over the body of the distribution
x = mmb_1 + u * s;
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Logarithmic.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Logarithmic.java?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Logarithmic.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Logarithmic.java Thu Sep 2 04:33:38 2010
@@ -8,8 +8,11 @@ It is provided "as is" without expressed
*/
package org.apache.mahout.math.jet.random;
+import org.apache.mahout.common.RandomUtils;
import org.apache.mahout.math.jet.random.engine.RandomEngine;
+import java.util.Random;
+
/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
@Deprecated
public class Logarithmic extends AbstractContinousDistribution {
@@ -22,10 +25,10 @@ public class Logarithmic extends Abstrac
private double a_prev = -1.0;
// The uniform random number generated shared by all <b>static</b> methods.
- private static final Logarithmic shared = new Logarithmic(0.5, makeDefaultGenerator());
+ private static final Logarithmic shared = new Logarithmic(0.5, RandomUtils.getRandom());
/** Constructs a Logarithmic distribution. */
- public Logarithmic(double p, RandomEngine randomGenerator) {
+ public Logarithmic(double p, Random randomGenerator) {
setRandomGenerator(randomGenerator);
setState(p);
}
@@ -81,7 +84,7 @@ public class Logarithmic extends Abstrac
}
}
- double u = randomGenerator.raw();
+ double u = randomGenerator.nextDouble();
int k;
if (a < 0.97) { // Inversion/Chop-down
k = 1;
@@ -97,7 +100,7 @@ public class Logarithmic extends Abstrac
if (u > a) {
return 1;
} // Transformation
- u = randomGenerator.raw();
+ u = randomGenerator.nextDouble();
double v = u;
double q = 1.0 - Math.exp(v * h);
if (u <= q * q) {
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/NegativeBinomial.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/NegativeBinomial.java?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/NegativeBinomial.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/NegativeBinomial.java Thu Sep 2 04:33:38 2010
@@ -29,6 +29,8 @@ import org.apache.mahout.math.jet.math.A
import org.apache.mahout.math.jet.random.engine.RandomEngine;
import org.apache.mahout.math.jet.stat.Probability;
+import java.util.Random;
+
/** Mostly deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
public class NegativeBinomial extends AbstractDiscreteDistribution {
@@ -47,7 +49,7 @@ public class NegativeBinomial extends Ab
* @param p the probability of success.
* @param randomGenerator a uniform random number generator.
*/
- public NegativeBinomial(int r, double p, RandomEngine randomGenerator) {
+ public NegativeBinomial(int r, double p, Random randomGenerator) {
setRandomGenerator(randomGenerator);
this.r = r;
this.p = p;
@@ -91,14 +93,6 @@ public class NegativeBinomial extends Ab
}
/**
- * Returns a random number from the distribution.
- */
- @Override
- public int nextInt() {
- return nextInt(r, p);
- }
-
- /**
* Returns a sample from this distribution. The value returned will
* be the number of negative samples required before achieving r
* positive samples. Each successive sample is taken independently
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Normal.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Normal.java?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Normal.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Normal.java Thu Sep 2 04:33:38 2010
@@ -12,6 +12,7 @@ import org.apache.mahout.math.jet.random
import org.apache.mahout.math.jet.stat.Probability;
import java.util.Locale;
+import java.util.Random;
/**
* Implements a normal distribution specified mean and standard deviation.
@@ -33,7 +34,7 @@ public class Normal extends AbstractCont
* @param randomGenerator The random number generator to use. This can be null if you don't
* need to generate any numbers.
*/
- public Normal(double mean, double standardDeviation, RandomEngine randomGenerator) {
+ public Normal(double mean, double standardDeviation, Random randomGenerator) {
setRandomGenerator(randomGenerator);
setState(mean, standardDeviation);
}
@@ -68,8 +69,8 @@ public class Normal extends AbstractCont
double y;
double r;
do {
- x = 2.0 * randomGenerator.raw() - 1.0;
- y = 2.0 * randomGenerator.raw() - 1.0;
+ x = 2.0 * randomGenerator.nextDouble() - 1.0;
+ y = 2.0 * randomGenerator.nextDouble() - 1.0;
r = x * x + y * y;
} while (r >= 1.0);
@@ -80,7 +81,7 @@ public class Normal extends AbstractCont
}
/** Sets the uniform random generator internally used. */
- public final void setRandomGenerator(RandomEngine randomGenerator) {
+ public final void setRandomGenerator(Random randomGenerator) {
super.setRandomGenerator(randomGenerator);
this.cacheFilled = false;
}
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Poisson.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Poisson.java?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Poisson.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Poisson.java Thu Sep 2 04:33:38 2010
@@ -12,6 +12,8 @@ import org.apache.mahout.math.jet.math.A
import org.apache.mahout.math.jet.random.engine.RandomEngine;
import org.apache.mahout.math.jet.stat.Probability;
+import java.util.Random;
+
/** Partially deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
public class Poisson extends AbstractDiscreteDistribution {
@@ -62,7 +64,7 @@ public class Poisson extends AbstractDis
/** Constructs a poisson distribution. Example: mean=1.0. */
- public Poisson(double mean, RandomEngine randomGenerator) {
+ public Poisson(double mean, Random randomGenerator) {
setRandomGenerator(randomGenerator);
this.mean = mean;
}
@@ -93,7 +95,7 @@ public class Poisson extends AbstractDis
return Math.exp(k * l_nu - Arithmetic.logFactorial(k) - c_pm);
}
- /** Returns a random number from the distribution. */
+ /** Returns a random number from the distribution.
@Override
public int nextInt() {
return nextInt(this.mean);
@@ -117,7 +119,7 @@ public class Poisson extends AbstractDis
* exponential functions. *
* *
*****************************************************************/
- RandomEngine gen = this.randomGenerator;
+ Random gen = this.randomGenerator;
//double t, g, my_k;
@@ -139,7 +141,7 @@ public class Poisson extends AbstractDis
}
m = (theMean > 1.0) ? (int) theMean : 1;
while (true) {
- double u = gen.raw();
+ double u = gen.nextDouble();
int k = 0;
if (u <= p0) {
return (k);
@@ -230,7 +232,7 @@ public class Poisson extends AbstractDis
int Y;
int X;
int Dk;
- if ((U = gen.raw() * p6) < p2) { // centre left
+ if ((U = gen.nextDouble() * p6) < p2) { // centre left
// immediate acceptance region R2 = [k2, m) *[0, f2), X = k2, ... m -1
if ((V = U - p1) < 0.0) {
@@ -243,7 +245,7 @@ public class Poisson extends AbstractDis
// computation of candidate X < k2, and its counterpart Y > k2
// either squeeze-acceptance of X or acceptance-rejection of Y
- Dk = (int) (dl * gen.raw()) + 1;
+ Dk = (int) (dl * gen.nextDouble()) + 1;
if (W <= f2 - Dk * (f2 - f2 / r2)) { // quick accept of
return (k2 - Dk); // X = k2 - Dk
}
@@ -269,7 +271,7 @@ public class Poisson extends AbstractDis
// computation of candidate X > k4, and its counterpart Y < k4
// either squeeze-acceptance of X or acceptance-rejection of Y
- Dk = (int) (dr * gen.raw()) + 1;
+ Dk = (int) (dr * gen.nextDouble()) + 1;
if (W <= f4 - Dk * (f4 - f4 * r4)) { // quick accept of
return (k4 + Dk); // X = k4 + Dk
}
@@ -284,7 +286,7 @@ public class Poisson extends AbstractDis
}
X = k4 + Dk;
} else {
- W = gen.raw();
+ W = gen.nextDouble();
if (U < p5) { // expon. tail left
Dk = (int) (1.0 - Math.log(W) / ll);
if ((X = k1 - Dk) < 0) {
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/PoissonSlow.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/PoissonSlow.java?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/PoissonSlow.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/PoissonSlow.java Thu Sep 2 04:33:38 2010
@@ -8,7 +8,9 @@ It is provided "as is" without expressed
*/
package org.apache.mahout.math.jet.random;
-import org.apache.mahout.math.jet.random.engine.RandomEngine;
+import org.apache.mahout.common.RandomUtils;
+
+import java.util.Random;
/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
@Deprecated
@@ -31,10 +33,10 @@ public class PoissonSlow extends Abstrac
0.1208650973866179e-2, -0.5395239384953e-5};
// The uniform random number generated shared by all <b>static</b> methods.
- private static final PoissonSlow shared = new PoissonSlow(0.0, makeDefaultGenerator());
+ private static final PoissonSlow shared = new PoissonSlow(0.0, RandomUtils.getRandom());
/** Constructs a poisson distribution. Example: mean=1.0. */
- public PoissonSlow(double mean, RandomEngine randomGenerator) {
+ public PoissonSlow(double mean, Random randomGenerator) {
setRandomGenerator(randomGenerator);
setMean(mean);
}
@@ -57,12 +59,6 @@ public class PoissonSlow extends Abstrac
return -tmp + Math.log(2.5066282746310005 * ser);
}
- /** Returns a random number from the distribution. */
- @Override
- public int nextInt() {
- return nextInt(this.mean);
- }
-
/** Returns a random number from the distribution; bypasses the internal state. */
private int nextInt(double xm) {
/*
@@ -78,7 +74,7 @@ public class PoissonSlow extends Abstrac
double product = 1;
do {
poisson++;
- product *= randomGenerator.raw();
+ product *= randomGenerator.nextDouble();
} while (product >= g);
// bug in CLHEP 1.4.0: was "} while ( product > g );"
return poisson;
@@ -88,16 +84,16 @@ public class PoissonSlow extends Abstrac
double sq = this.cached_sq;
double alxm = this.cached_alxm;
- RandomEngine rand = this.randomGenerator;
+ Random rand = this.randomGenerator;
do {
double y;
do {
- y = Math.tan(Math.PI * rand.raw());
+ y = Math.tan(Math.PI * rand.nextDouble());
em = sq * y + xm;
} while (em < 0.0);
em = (double) (int) (em); // faster than em = Math.floor(em); (em>=0.0)
t = 0.9 * (1.0 + y * y) * Math.exp(em * alxm - logGamma(em + 1.0) - g);
- } while (rand.raw() > t);
+ } while (rand.nextDouble() > t);
return (int) em;
} else { // mean is too large
return (int) xm;
@@ -110,7 +106,7 @@ public class PoissonSlow extends Abstrac
int count = 0;
double product;
for (product = 1.0; product >= bound && product > 0.0; count++) {
- product *= randomGenerator.raw();
+ product *= randomGenerator.nextDouble();
}
if (product <= 0.0 && bound > 0.0) {
return (int) Math.round(mean);
@@ -135,14 +131,6 @@ public class PoissonSlow extends Abstrac
}
}
- /** Returns a random number from the distribution with the given mean. */
- public static int staticNextInt(double mean) {
- synchronized (shared) {
- shared.setMean(mean);
- return shared.nextInt();
- }
- }
-
/** Returns a String representation of the receiver. */
public String toString() {
return this.getClass().getName() + '(' + mean + ')';
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/StudentT.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/StudentT.java?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/StudentT.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/StudentT.java Thu Sep 2 04:33:38 2010
@@ -8,9 +8,12 @@ It is provided "as is" without expressed
*/
package org.apache.mahout.math.jet.random;
+import org.apache.mahout.common.RandomUtils;
import org.apache.mahout.math.jet.random.engine.RandomEngine;
import org.apache.mahout.math.jet.stat.Probability;
+import java.util.Random;
+
/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
@Deprecated
public class StudentT extends AbstractContinousDistribution {
@@ -19,7 +22,7 @@ public class StudentT extends AbstractCo
private double TERM; // performance cache for pdf()
// The uniform random number generated shared by all <b>static</b> methods.
- private static final StudentT shared = new StudentT(1.0, makeDefaultGenerator());
+ private static final StudentT shared = new StudentT(1.0, RandomUtils.getRandom());
/**
* Constructs a StudentT distribution. Example: freedom=1.0.
@@ -27,7 +30,7 @@ public class StudentT extends AbstractCo
* @param freedom degrees of freedom.
* @throws IllegalArgumentException if <tt>freedom <= 0.0</tt>.
*/
- public StudentT(double freedom, RandomEngine randomGenerator) {
+ public StudentT(double freedom, Random randomGenerator) {
setRandomGenerator(randomGenerator);
setState(freedom);
}
@@ -68,8 +71,8 @@ public class StudentT extends AbstractCo
double w;
do {
- u = 2.0 * randomGenerator.raw() - 1.0;
- v = 2.0 * randomGenerator.raw() - 1.0;
+ u = 2.0 * randomGenerator.nextDouble() - 1.0;
+ v = 2.0 * randomGenerator.nextDouble() - 1.0;
}
while ((w = u * u + v * v) > 1.0);
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Uniform.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Uniform.java?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Uniform.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Uniform.java Thu Sep 2 04:33:38 2010
@@ -8,8 +8,9 @@ It is provided "as is" without expressed
*/
package org.apache.mahout.math.jet.random;
-import org.apache.mahout.math.jet.random.engine.MersenneTwister;
-import org.apache.mahout.math.jet.random.engine.RandomEngine;
+import org.apache.mahout.common.RandomUtils;
+
+import java.util.Random;
public class Uniform extends AbstractContinousDistribution {
@@ -17,24 +18,24 @@ public class Uniform extends AbstractCon
private double max;
// The uniform random number generated shared by all <b>static</b> methods.
- protected static final Uniform shared = new Uniform(makeDefaultGenerator());
+ protected static final Uniform shared = new Uniform(RandomUtils.getRandom());
/**
* Constructs a uniform distribution with the given minimum and maximum, using a {@link
* org.apache.mahout.math.jet.random.engine.MersenneTwister} seeded with the given seed.
*/
public Uniform(double min, double max, int seed) {
- this(min, max, new MersenneTwister(seed));
+ this(min, max, RandomUtils.getRandom(seed));
}
/** Constructs a uniform distribution with the given minimum and maximum. */
- public Uniform(double min, double max, RandomEngine randomGenerator) {
+ public Uniform(double min, double max, Random randomGenerator) {
setRandomGenerator(randomGenerator);
setState(min, max);
}
/** Constructs a uniform distribution with <tt>min=0.0</tt> and <tt>max=1.0</tt>. */
- public Uniform(RandomEngine randomGenerator) {
+ public Uniform(Random randomGenerator) {
this(0, 1, randomGenerator);
}
@@ -51,7 +52,7 @@ public class Uniform extends AbstractCon
/** Returns a uniformly distributed random <tt>boolean</tt>. */
public boolean nextBoolean() {
- return randomGenerator.raw() > 0.5;
+ return randomGenerator.nextDouble() > 0.5;
}
/**
@@ -60,7 +61,7 @@ public class Uniform extends AbstractCon
*/
@Override
public double nextDouble() {
- return min + (max - min) * randomGenerator.raw();
+ return min + (max - min) * randomGenerator.nextDouble();
}
/**
@@ -68,7 +69,7 @@ public class Uniform extends AbstractCon
* <tt>to</tt>). Pre conditions: <tt>from <= to</tt>.
*/
public double nextDoubleFromTo(double from, double to) {
- return from + (to - from) * randomGenerator.raw();
+ return from + (to - from) * randomGenerator.nextDouble();
}
/**
@@ -80,21 +81,12 @@ public class Uniform extends AbstractCon
}
/**
- * Returns a uniformly distributed random number in the closed interval <tt>[min,max]</tt> (including <tt>min</tt> and
- * <tt>max</tt>).
- */
- @Override
- public int nextInt() {
- return nextIntFromTo((int) Math.round(min), (int) Math.round(max));
- }
-
- /**
* Returns a uniformly distributed random number in the closed interval
* <tt>[from,to]</tt> (including <tt>from</tt>
* and <tt>to</tt>). Pre conditions: <tt>from <= to</tt>.
*/
public int nextIntFromTo(int from, int to) {
- return (int) ((long) from + (long) ((1L + (long) to - (long) from) * randomGenerator.raw()));
+ return (int) ((long) from + (long) ((1L + (long) to - (long) from) * randomGenerator.nextDouble()));
}
/**
@@ -221,17 +213,6 @@ public class Uniform extends AbstractCon
}
}
- /**
- * Sets the uniform random number generation engine shared by all <b>static</b> methods.
- *
- * @param randomGenerator the new uniform random number generation engine to be shared.
- */
- public static void staticSetRandomEngine(RandomEngine randomGenerator) {
- synchronized (shared) {
- shared.setRandomGenerator(randomGenerator);
- }
- }
-
/** Returns a String representation of the receiver. */
public String toString() {
return this.getClass().getName() + '(' + min + ',' + max + ')';
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/VonMises.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/VonMises.java?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/VonMises.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/VonMises.java Thu Sep 2 04:33:38 2010
@@ -8,8 +8,11 @@ It is provided "as is" without expressed
*/
package org.apache.mahout.math.jet.random;
+import org.apache.mahout.common.RandomUtils;
import org.apache.mahout.math.jet.random.engine.RandomEngine;
+import java.util.Random;
+
/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
@Deprecated
public class VonMises extends AbstractContinousDistribution {
@@ -21,14 +24,14 @@ public class VonMises extends AbstractCo
private double r;
// The uniform random number generated shared by all <b>static</b> methods.
- private static final VonMises shared = new VonMises(1.0, makeDefaultGenerator());
+ private static final VonMises shared = new VonMises(1.0, RandomUtils.getRandom());
/**
* Constructs a Von Mises distribution. Example: k=1.0.
*
* @throws IllegalArgumentException if <tt>k <= 0.0</tt>.
*/
- public VonMises(double freedom, RandomEngine randomGenerator) {
+ public VonMises(double freedom, Random randomGenerator) {
setRandomGenerator(randomGenerator);
setState(freedom);
}
@@ -80,14 +83,14 @@ public class VonMises extends AbstractCo
double w;
double v;
do {
- double u = randomGenerator.raw();
- v = randomGenerator.raw(); // U(0/1)
+ double u = randomGenerator.nextDouble();
+ v = randomGenerator.nextDouble(); // U(0/1)
double z = Math.cos(Math.PI * u);
w = (1.0 + r * z) / (r + z);
c = k * (r - w);
} while ((c * (2.0 - c) < v) && (Math.log(c / v) + 1.0 < c)); // Acceptance/Rejection
- return (randomGenerator.raw() > 0.5) ? Math.acos(w) : -Math.acos(w); // Random sign //
+ return (randomGenerator.nextDouble() > 0.5) ? Math.acos(w) : -Math.acos(w); // Random sign //
// 0 <= x <= Pi : -Pi <= x <= 0 //
}
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Zeta.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Zeta.java?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Zeta.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Zeta.java Thu Sep 2 04:33:38 2010
@@ -8,8 +8,11 @@ It is provided "as is" without expressed
*/
package org.apache.mahout.math.jet.random;
+import org.apache.mahout.common.RandomUtils;
import org.apache.mahout.math.jet.random.engine.RandomEngine;
+import java.util.Random;
+
/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
@Deprecated
public class Zeta extends AbstractDiscreteDistribution {
@@ -25,16 +28,16 @@ public class Zeta extends AbstractDiscre
private static final double maxlongint = Long.MAX_VALUE - 1.5;
// The uniform random number generated shared by all <b>static</b> methods.
- private static final Zeta shared = new Zeta(1.0, 1.0, makeDefaultGenerator());
+ private static final Zeta shared = new Zeta(1.0, 1.0, RandomUtils.getRandom());
/** Constructs a Zeta distribution. */
- public Zeta(double ro, double pk, RandomEngine randomGenerator) {
+ public Zeta(double ro, double pk, Random randomGenerator) {
setRandomGenerator(randomGenerator);
setState(ro, pk);
}
/** Returns a zeta distributed random number. */
- protected long generateZeta(double ro, double pk, RandomEngine randomGenerator) {
+ protected long generateZeta(double ro, double pk, Random randomGenerator) {
/******************************************************************
* *
* Zeta Distribution - Acceptance Rejection *
@@ -91,8 +94,8 @@ public class Zeta extends AbstractDiscre
do {
double v;
do {
- double u = randomGenerator.raw();
- v = randomGenerator.raw();
+ double u = randomGenerator.nextDouble();
+ v = randomGenerator.nextDouble();
x = (c + 0.5) * Math.exp(-Math.log(u) / ro) - c;
} while (x <= 0.5 || x >= maxlongint);
@@ -103,10 +106,10 @@ public class Zeta extends AbstractDiscre
return k;
}
- /** Returns a random number from the distribution. */
+ /** Returns a random number from the distribution.
@Override
public int nextInt() {
- return (int) generateZeta(ro, pk, randomGenerator);
+ return (int) generateZeta(ro, pk, this.randomGenerator);
}
/** Sets the parameters. */
@@ -115,14 +118,6 @@ public class Zeta extends AbstractDiscre
this.pk = pk;
}
- /** Returns a random number from the distribution. */
- public static int staticNextInt(double ro, double pk) {
- synchronized (shared) {
- shared.setState(ro, pk);
- return shared.nextInt();
- }
- }
-
/** Returns a String representation of the receiver. */
public String toString() {
return this.getClass().getName() + '(' + ro + ',' + pk + ')';
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/RandomSampler.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/RandomSampler.java?rev=991806&r1=991805&r2=991806&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/RandomSampler.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/RandomSampler.java Thu Sep 2 04:33:38 2010
@@ -8,9 +8,12 @@ It is provided "as is" without expressed
*/
package org.apache.mahout.math.jet.random.sampling;
+import org.apache.mahout.common.RandomUtils;
import org.apache.mahout.math.PersistentObject;
import org.apache.mahout.math.jet.random.engine.RandomEngine;
+import java.util.Random;
+
/**
* Space and time efficiently computes a sorted <i>Simple Random Sample Without Replacement (SRSWOR)</i>, that is, a sorted set of <tt>n</tt> random numbers from an interval of <tt>N</tt> numbers;
* Example: Computing <tt>n=3</tt> random numbers from the interval <tt>[1,50]</tt> may yield the sorted random set <tt>(7,13,47)</tt>.
@@ -115,7 +118,7 @@ public class RandomSampler extends Persi
private long n;
private long N;
private long low;
- private RandomEngine randomGenerator;
+ private Random randomGenerator;
/**
* Constructs a random sampler that computes and delivers sorted random sets in blocks. A set block can be retrieved
@@ -129,7 +132,7 @@ public class RandomSampler extends Persi
* @param randomGenerator a random number generator. Set this parameter to <tt>null</tt> to use the default random
* number generator.
*/
- public RandomSampler(long n, long N, long low, RandomEngine randomGenerator) {
+ public RandomSampler(long n, long N, long low, Random randomGenerator) {
if (n < 0) {
throw new IllegalArgumentException("n must be >= 0");
}
@@ -141,23 +144,15 @@ public class RandomSampler extends Persi
this.low = low;
if (randomGenerator == null) {
- randomGenerator = org.apache.mahout.math.jet.random.AbstractDistribution.makeDefaultGenerator();
+ randomGenerator = RandomUtils.getRandom();
}
this.randomGenerator = randomGenerator;
}
- RandomEngine getRandomGenerator() {
+ Random getRandomGenerator() {
return randomGenerator;
}
- /** Returns a deep copy of the receiver. */
- @Override
- public Object clone() {
- RandomSampler copy = (RandomSampler) super.clone();
- copy.randomGenerator = (RandomEngine) this.randomGenerator.clone();
- return copy;
- }
-
/**
* Computes the next <tt>count</tt> random numbers of the sorted random set specified on instance construction and
* fills them into <tt>values</tt>, starting at index <tt>fromIndex</tt>.
@@ -202,16 +197,16 @@ public class RandomSampler extends Persi
* @param n the total number of elements to choose (must be >= 0).
* @param N the interval to choose random numbers from is <tt>[low,low+N-1]</tt>.
* @param count the number of elements to be filled into <tt>values</tt> by this call (must be >= 0 and
- * <=<tt>n</tt>). Normally, you will set <tt>count=n</tt>.
+* <=<tt>n</tt>). Normally, you will set <tt>count=n</tt>.
* @param low the interval to choose random numbers from is <tt>[low,low+N-1]</tt>. Hint: If
- * <tt>low==0</tt>, then draws random numbers from the interval <tt>[0,N-1]</tt>.
+* <tt>low==0</tt>, then draws random numbers from the interval <tt>[0,N-1]</tt>.
* @param values the array into which the random numbers are to be filled; must have a length <tt>>=
- * count+fromIndex</tt>.
+* count+fromIndex</tt>.
* @param fromIndex the first index within <tt>values</tt> to be filled with numbers (inclusive).
* @param randomGenerator a random number generator.
*/
protected static void rejectMethodD(long n, long N, int count, long low, long[] values, int fromIndex,
- RandomEngine randomGenerator) {
+ Random randomGenerator) {
/* This algorithm is applicable if a large percentage (90%..100%) of N shall be sampled.
In such cases it is more efficient than sampleMethodA() and sampleMethodD().
The idea is that it is more efficient to express
@@ -232,7 +227,7 @@ public class RandomSampler extends Persi
double nreal = n;
double ninv = 1.0 / nreal;
double Nreal = N;
- double Vprime = Math.exp(Math.log(randomGenerator.raw()) * ninv);
+ double Vprime = Math.exp(Math.log(randomGenerator.nextDouble()) * ninv);
long qu1 = -n + 1 + N;
double qu1real = -nreal + 1.0 + Nreal;
//threshold = -negalphainv * n;
@@ -249,9 +244,9 @@ public class RandomSampler extends Persi
if (S < qu1) {
break;
}
- Vprime = Math.exp(Math.log(randomGenerator.raw()) * ninv);
+ Vprime = Math.exp(Math.log(randomGenerator.nextDouble()) * ninv);
}
- double U = randomGenerator.raw();
+ double U = randomGenerator.nextDouble();
negSreal = -S;
//step D3: Accept?
@@ -280,10 +275,10 @@ public class RandomSampler extends Persi
}
if (Nreal / (-X + Nreal) >= y1 * Math.exp(Math.log(y2) * nmin1inv)) {
// accept !
- Vprime = Math.exp(Math.log(randomGenerator.raw()) * nmin1inv);
+ Vprime = Math.exp(Math.log(randomGenerator.nextDouble()) * nmin1inv);
break; //break inner loop
}
- Vprime = Math.exp(Math.log(randomGenerator.raw()) * ninv);
+ Vprime = Math.exp(Math.log(randomGenerator.nextDouble()) * ninv);
}
//step D5: reject the (S+1)st record !
@@ -359,7 +354,7 @@ public class RandomSampler extends Persi
* number generator.
*/
public static void sample(long n, long N, int count, long low, long[] values, int fromIndex,
- RandomEngine randomGenerator) {
+ Random randomGenerator) {
if (n <= 0 || count <= 0) {
return;
}
@@ -367,7 +362,7 @@ public class RandomSampler extends Persi
throw new IllegalArgumentException("count must not be greater than n");
}
if (randomGenerator == null) {
- randomGenerator = org.apache.mahout.math.jet.random.AbstractDistribution.makeDefaultGenerator();
+ randomGenerator = RandomUtils.getRandom();
}
if (count == N) { // rare case treated quickly
@@ -400,23 +395,23 @@ public class RandomSampler extends Persi
* @param n the total number of elements to choose (must be >= 0).
* @param N the interval to choose random numbers from is <tt>[low,low+N-1]</tt>.
* @param count the number of elements to be filled into <tt>values</tt> by this call (must be >= 0 and
- * <=<tt>n</tt>). Normally, you will set <tt>count=n</tt>.
+* <=<tt>n</tt>). Normally, you will set <tt>count=n</tt>.
* @param low the interval to choose random numbers from is <tt>[low,low+N-1]</tt>. Hint: If
- * <tt>low==0</tt>, then draws random numbers from the interval <tt>[0,N-1]</tt>.
+* <tt>low==0</tt>, then draws random numbers from the interval <tt>[0,N-1]</tt>.
* @param values the array into which the random numbers are to be filled; must have a length <tt>>=
- * count+fromIndex</tt>.
+* count+fromIndex</tt>.
* @param fromIndex the first index within <tt>values</tt> to be filled with numbers (inclusive).
* @param randomGenerator a random number generator.
*/
protected static void sampleMethodA(long n, long N, int count, long low, long[] values, int fromIndex,
- RandomEngine randomGenerator) {
+ Random randomGenerator) {
long chosen = -1 + low;
double top = N - n;
double Nreal = N;
long S;
while (n >= 2 && count > 0) {
- double V = randomGenerator.raw();
+ double V = randomGenerator.nextDouble();
S = 0;
double quot = top / Nreal;
while (quot > V) {
@@ -434,7 +429,7 @@ public class RandomSampler extends Persi
if (count > 0) {
// special case n==1
- S = (long) (Math.round(Nreal) * randomGenerator.raw());
+ S = (long) (Math.round(Nreal) * randomGenerator.nextDouble());
chosen += S + 1;
values[fromIndex] = chosen;
}
@@ -452,22 +447,22 @@ public class RandomSampler extends Persi
* @param n the total number of elements to choose (must be >= 0).
* @param N the interval to choose random numbers from is <tt>[low,low+N-1]</tt>.
* @param count the number of elements to be filled into <tt>values</tt> by this call (must be >= 0 and
- * <=<tt>n</tt>). Normally, you will set <tt>count=n</tt>.
+* <=<tt>n</tt>). Normally, you will set <tt>count=n</tt>.
* @param low the interval to choose random numbers from is <tt>[low,low+N-1]</tt>. Hint: If
- * <tt>low==0</tt>, then draws random numbers from the interval <tt>[0,N-1]</tt>.
+* <tt>low==0</tt>, then draws random numbers from the interval <tt>[0,N-1]</tt>.
* @param values the array into which the random numbers are to be filled; must have a length <tt>>=
- * count+fromIndex</tt>.
+* count+fromIndex</tt>.
* @param fromIndex the first index within <tt>values</tt> to be filled with numbers (inclusive).
* @param randomGenerator a random number generator.
*/
protected static void sampleMethodD(long n, long N, int count, long low, long[] values, int fromIndex,
- RandomEngine randomGenerator) {
+ Random randomGenerator) {
long chosen = -1 + low;
double nreal = n;
double ninv = 1.0 / nreal;
double Nreal = N;
- double Vprime = Math.exp(Math.log(randomGenerator.raw()) * ninv);
+ double Vprime = Math.exp(Math.log(randomGenerator.nextDouble()) * ninv);
long qu1 = -n + 1 + N;
double qu1real = -nreal + 1.0 + Nreal;
long negalphainv =
@@ -486,9 +481,9 @@ public class RandomSampler extends Persi
if (S < qu1) {
break;
}
- Vprime = Math.exp(Math.log(randomGenerator.raw()) * ninv);
+ Vprime = Math.exp(Math.log(randomGenerator.nextDouble()) * ninv);
}
- double U = randomGenerator.raw();
+ double U = randomGenerator.nextDouble();
negSreal = -S;
//step D3: Accept?
@@ -517,10 +512,10 @@ public class RandomSampler extends Persi
}
if (Nreal / (-X + Nreal) >= y1 * Math.exp(Math.log(y2) * nmin1inv)) {
// accept !
- Vprime = Math.exp(Math.log(randomGenerator.raw()) * nmin1inv);
+ Vprime = Math.exp(Math.log(randomGenerator.nextDouble()) * nmin1inv);
break; //break inner loop
}
- Vprime = Math.exp(Math.log(randomGenerator.raw()) * ninv);
+ Vprime = Math.exp(Math.log(randomGenerator.nextDouble()) * ninv);
}
//step D5: select the (S+1)st record !