commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject svn commit: r1213081 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/random/ site/xdoc/ test/java/org/apache/commons/math/random/
Date Sun, 11 Dec 2011 21:33:38 GMT
Author: psteitz
Date: Sun Dec 11 21:33:37 2011
New Revision: 1213081

URL: http://svn.apache.org/viewvc?rev=1213081&view=rev
Log:
Modified setSeed methods in Bitstream generators (MersenneTwister and Well
generators) to clear the cache of normal deviates used by nextGaussian.

JIRA: MATH-723

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/AbstractWell.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/BitsStreamGenerator.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/MersenneTwister.java
    commons/proper/math/trunk/src/site/xdoc/changes.xml
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/BitsStreamGeneratorTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomGeneratorAbstractTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/AbstractWell.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/AbstractWell.java?rev=1213081&r1=1213080&r2=1213081&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/AbstractWell.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/AbstractWell.java
Sun Dec 11 21:33:37 2011
@@ -151,7 +151,6 @@ public abstract class AbstractWell exten
      */
     @Override
     public void setSeed(final int[] seed) {
-
         if (seed == null) {
             setSeed(System.currentTimeMillis() + System.identityHashCode(this));
             return;
@@ -167,7 +166,7 @@ public abstract class AbstractWell exten
         }
 
         index = 0;
-
+        clear();  // Clear normal deviate cache
     }
 
     /** Reinitialize the generator as if just built with the given long seed.

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/BitsStreamGenerator.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/BitsStreamGenerator.java?rev=1213081&r1=1213080&r2=1213081&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/BitsStreamGenerator.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/BitsStreamGenerator.java
Sun Dec 11 21:33:37 2011
@@ -157,4 +157,12 @@ public abstract class BitsStreamGenerato
         return high | low;
     }
 
+    /**
+     * Clears the cache used by the default implementation of
+     * {@link #nextGaussian}.
+     */
+    public void clear() {
+        nextGaussian = Double.NaN;
+    }
+
 }

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/MersenneTwister.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/MersenneTwister.java?rev=1213081&r1=1213080&r2=1213081&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/MersenneTwister.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/MersenneTwister.java
Sun Dec 11 21:33:37 2011
@@ -150,6 +150,8 @@ public class MersenneTwister extends Bit
             longMT = (1812433253l * (longMT ^ (longMT >> 30)) + mti) & 0xffffffffL;
             mt[mti]= (int) longMT;
         }
+
+        clear(); // Clear normal deviate cache
     }
 
     /** Reinitialize the generator as if just built with the given int array seed.
@@ -200,6 +202,8 @@ public class MersenneTwister extends Bit
 
         mt[0] = 0x80000000; // MSB is 1; assuring non-zero initial array
 
+        clear(); // Clear normal deviate cache
+
     }
 
     /** Reinitialize the generator as if just built with the given long seed.

Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=1213081&r1=1213080&r2=1213081&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Sun Dec 11 21:33:37 2011
@@ -52,6 +52,10 @@ The <action> type attribute can be add,u
     If the output is not quite correct, check for invisible trailing spaces!
      -->
     <release version="3.0" date="TBD" description="TBD">
+      <action dev="psteitz" type="fix" issue="MATH-723">
+        Modified setSeed methods in Bitstream generators (MersenneTwister and Well
+        generators) to clear the cache of normal deviates used by nextGaussian.
+      </action>
       <action dev="luc" type="fix" issue="MATH-716">
         Fixed bracketing interval balancing in BracketingNthOrderBrentSolver.
       </action>

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/BitsStreamGeneratorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/BitsStreamGeneratorTest.java?rev=1213081&r1=1213080&r2=1213081&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/BitsStreamGeneratorTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/BitsStreamGeneratorTest.java
Sun Dec 11 21:33:37 2011
@@ -46,7 +46,8 @@ public class BitsStreamGeneratorTest ext
         
         @Override
         public void setSeed(int seed) {
-           ran.setSeed(seed);   
+           ran.setSeed(seed);
+           clear();
         }
 
         @Override

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomGeneratorAbstractTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomGeneratorAbstractTest.java?rev=1213081&r1=1213080&r2=1213081&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomGeneratorAbstractTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomGeneratorAbstractTest.java
Sun Dec 11 21:33:37 2011
@@ -335,5 +335,73 @@ public abstract class RandomGeneratorAbs
         TestUtils.assertChiSquareAccept(expected, count, 0.001);
         
     }
+    
+    @Test
+    public void testSeeding() throws Exception {
+        // makeGenerator initializes with fixed seed
+        RandomGenerator gen = makeGenerator();
+        RandomGenerator gen1 = makeGenerator();
+        checkSameSequence(gen, gen1);
+        // reseed, but recreate the second one
+        // verifies MATH-723
+        gen.setSeed(100);
+        gen1 = makeGenerator();
+        gen1.setSeed(100);
+        checkSameSequence(gen, gen1);
+    }
+    
+    private void checkSameSequence(RandomGenerator gen1, RandomGenerator gen2) throws Exception
{
+        final int len = 11;  // Needs to be an odd number to check MATH-723
+        final double[][] values = new double[2][len];
+        for (int i = 0; i < len; i++) {
+            values[0][i] = gen1.nextDouble();
+        }
+        for (int i = 0; i < len; i++) {
+            values[1][i] = gen2.nextDouble();
+        }
+        Assert.assertTrue(Arrays.equals(values[0], values[1])); 
+        for (int i = 0; i < len; i++) {
+            values[0][i] = gen1.nextFloat();
+        }
+        for (int i = 0; i < len; i++) {
+            values[1][i] = gen2.nextFloat();
+        }
+        Assert.assertTrue(Arrays.equals(values[0], values[1])); 
+        for (int i = 0; i < len; i++) {
+            values[0][i] = gen1.nextInt();
+        }
+        for (int i = 0; i < len; i++) {
+            values[1][i] = gen2.nextInt();
+        }
+        Assert.assertTrue(Arrays.equals(values[0], values[1])); 
+        for (int i = 0; i < len; i++) {
+            values[0][i] = gen1.nextLong();
+        }
+        for (int i = 0; i < len; i++) {
+            values[1][i] = gen2.nextLong();
+        }
+        Assert.assertTrue(Arrays.equals(values[0], values[1])); 
+        for (int i = 0; i < len; i++) {
+            values[0][i] = gen1.nextInt(len);
+        }
+        for (int i = 0; i < len; i++) {
+            values[1][i] = gen2.nextInt(len);
+        }
+        Assert.assertTrue(Arrays.equals(values[0], values[1])); 
+        for (int i = 0; i < len; i++) {
+            values[0][i] = gen1.nextBoolean() ? 1 : 0;
+        }
+        for (int i = 0; i < len; i++) {
+            values[1][i] = gen2.nextBoolean() ? 1 : 0;
+        }
+        Assert.assertTrue(Arrays.equals(values[0], values[1]));  
+        for (int i = 0; i < len; i++) {
+            values[0][i] = gen1.nextGaussian();
+        }
+        for (int i = 0; i < len; i++) {
+            values[1][i] = gen2.nextGaussian();
+        }
+        Assert.assertTrue(Arrays.equals(values[0], values[1])); 
+    }
 
 }



Mime
View raw message