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 Hendriks in JIRA: MATH-720.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1213130 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
1352a70f26
commit
fe1b30b589
|
@ -157,7 +157,12 @@ public class RandomDataImpl implements RandomData, Serializable {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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 RandomData, Serializable {
|
|||
* 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,11 +841,19 @@ public class RandomDataImpl implements RandomData, Serializable {
|
|||
*/
|
||||
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>
|
||||
|
@ -869,7 +882,7 @@ public class RandomDataImpl implements RandomData, Serializable {
|
|||
*/
|
||||
public void reSeed(long seed) {
|
||||
if (rand == null) {
|
||||
rand = new JDKRandomGenerator();
|
||||
initRan();
|
||||
}
|
||||
rand.setSeed(seed);
|
||||
}
|
||||
|
@ -905,14 +918,14 @@ public class RandomDataImpl implements RandomData, Serializable {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue