Author: psteitz
Date: Mon Dec 12 01:31:14 2011
New Revision: 1213130
URL: http://svn.apache.org/viewvc?rev=1213130&view=rev
Log:
Made RandomDataImpl consistently use a Well generator as the default random
generator. This completes the fix for JIRA: MATH-701. The inconsistency was
reported by Dennis Hendricks in JIRA: MATH-720.
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/RandomDataImpl.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/RandomDataImpl.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/RandomDataImpl.java?rev=1213130&r1=1213129&r2=1213130&view=diff
==============================================================================
---
commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/RandomDataImpl.java
(original)
+++
commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/RandomDataImpl.java
Mon Dec 12 01:31:14 2011
@@ -157,7 +157,12 @@ public class RandomDataImpl implements R
}
/**
- * Construct a RandomDataImpl.
+ * Construct a RandomDataImpl, using a default random generator as the
source
+ * of randomness.
+ *
+ * <p>The default generator is a {@link Well19937c} seeded
+ * with {@code System.currentTimeMillis() +
System.identityHashCode(this))}.
+ * The generator is initialized and seeded on first use.</p>
*/
public RandomDataImpl() {
}
@@ -167,7 +172,7 @@ public class RandomDataImpl implements R
* the source of (non-secure) random data.
*
* @param rand the source of (non-secure) random data
- * (may be null, resulting in default JDK-supplied generator)
+ * (may be null, resulting in the default generator)
* @since 1.1
*/
public RandomDataImpl(RandomGenerator rand) {
@@ -836,12 +841,20 @@ public class RandomDataImpl implements R
*/
private RandomGenerator getRan() {
if (rand == null) {
- rand = new Well19937c(System.currentTimeMillis() +
System.identityHashCode(this));
+ initRan();
}
return rand;
}
/**
+ * Sets the default generator to a {@link Well19937c} generator seeded with
+ * {@code System.currentTimeMillis() + System.identityHashCode(this))}.
+ */
+ private void initRan() {
+ rand = new Well19937c(System.currentTimeMillis() +
System.identityHashCode(this));
+ }
+
+ /**
* Returns the SecureRandom used to generate secure random data.
* <p>
* Creates and initializes if null. Uses
@@ -869,7 +882,7 @@ public class RandomDataImpl implements R
*/
public void reSeed(long seed) {
if (rand == null) {
- rand = new JDKRandomGenerator();
+ initRan();
}
rand.setSeed(seed);
}
@@ -905,14 +918,14 @@ public class RandomDataImpl implements R
}
/**
- * Reseeds the random number generator with the current time in
- * milliseconds.
+ * Reseeds the random number generator with
+ * {@code System.currentTimeMillis() + System.identityHashCode(this))}.
*/
public void reSeed() {
if (rand == null) {
- rand = new JDKRandomGenerator();
+ initRan();
}
- rand.setSeed(System.currentTimeMillis());
+ rand.setSeed(System.currentTimeMillis() +
System.identityHashCode(this));
}
/**
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java?rev=1213130&r1=1213129&r2=1213130&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java
Mon Dec 12 01:31:14 2011
@@ -1056,5 +1056,18 @@ public class RandomDataTest {
}
TestUtils.assertChiSquareAccept(densityPoints, expectedCounts,
observedCounts, .001);
}
+
+ @Test
+ /**
+ * MATH-720
+ */
+ public void testReseed() {
+ PoissonDistribution x = new PoissonDistribution(3.0);
+ x.reseedRandomGenerator(0);
+ final double u = x.sample();
+ PoissonDistribution y = new PoissonDistribution(3.0);
+ y.reseedRandomGenerator(0);
+ Assert.assertEquals(u, y.sample(), 0);
+ }
}