[Numpy-svn] r8444 - trunk/numpy/random/mtrand

numpy-svn at scipy.org numpy-svn at scipy.org
Tue May 25 22:26:17 EDT 2010


Author: charris
Date: 2010-05-25 21:26:17 -0500 (Tue, 25 May 2010)
New Revision: 8444

Modified:
   trunk/numpy/random/mtrand/initarray.c
   trunk/numpy/random/mtrand/randomkit.c
Log:
BUG, STY: Make gaussian random number generators with identical behaviour
have identical pickles.

Modified: trunk/numpy/random/mtrand/initarray.c
===================================================================
--- trunk/numpy/random/mtrand/initarray.c	2010-05-26 02:26:14 UTC (rev 8443)
+++ trunk/numpy/random/mtrand/initarray.c	2010-05-26 02:26:17 UTC (rev 8444)
@@ -145,6 +145,7 @@
     }
 
     mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
+    self->gauss = 0;
     self->has_gauss = 0;
     self->has_binomial = 0;
 }

Modified: trunk/numpy/random/mtrand/randomkit.c
===================================================================
--- trunk/numpy/random/mtrand/randomkit.c	2010-05-26 02:26:14 UTC (rev 8443)
+++ trunk/numpy/random/mtrand/randomkit.c	2010-05-26 02:26:17 UTC (rev 8444)
@@ -127,8 +127,8 @@
 
 char *rk_strerror[RK_ERR_MAX] =
 {
-        "no error",
-        "random device unvavailable"
+    "no error",
+    "random device unvavailable"
 };
 
 /* static functions */
@@ -137,17 +137,18 @@
 void
 rk_seed(unsigned long seed, rk_state *state)
 {
-        int pos;
-        seed &= 0xffffffffUL;
+    int pos;
+    seed &= 0xffffffffUL;
 
-        /* Knuth's PRNG as used in the Mersenne Twister reference implementation */
-        for (pos = 0; pos < RK_STATE_LEN; pos++) {
-            state->key[pos] = seed;
-            seed = (1812433253UL * (seed ^ (seed >> 30)) + pos + 1) & 0xffffffffUL;
-        }
-        state->pos = RK_STATE_LEN;
-        state->has_gauss = 0;
-        state->has_binomial = 0;
+    /* Knuth's PRNG as used in the Mersenne Twister reference implementation */
+    for (pos = 0; pos < RK_STATE_LEN; pos++) {
+        state->key[pos] = seed;
+        seed = (1812433253UL * (seed ^ (seed >> 30)) + pos + 1) & 0xffffffffUL;
+    }
+    state->pos = RK_STATE_LEN;
+    state->gauss = 0;
+    state->has_gauss = 0;
+    state->has_binomial = 0;
 }
 
 /* Thomas Wang 32 bits integer hash function */
@@ -177,6 +178,7 @@
         /* ensures non-zero key */
         state->key[0] |= 0x80000000UL;
         state->pos = RK_STATE_LEN;
+        state->gauss = 0;
         state->has_gauss = 0;
         state->has_binomial = 0;
 
@@ -375,8 +377,10 @@
 rk_gauss(rk_state *state)
 {
     if (state->has_gauss) {
+        const double tmp = state->gauss;
+        state->gauss = 0;
         state->has_gauss = 0;
-        return state->gauss;
+        return tmp;
     }
     else {
         double f, x1, x2, r2;
@@ -390,9 +394,9 @@
 
         /* Box-Muller transform */
         f = sqrt(-2.0*log(r2)/r2);
-        state->has_gauss = 1;
         /* Keep for next call */
         state->gauss = f*x1;
+        state->has_gauss = 1;
         return f*x2;
     }
 }




More information about the Numpy-svn mailing list