summaryrefslogtreecommitdiff
path: root/numpy/random
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2010-05-26 02:26:17 +0000
committerCharles Harris <charlesr.harris@gmail.com>2010-05-26 02:26:17 +0000
commitdccd8d0d688d479dfe26361eb3f18745fe690c10 (patch)
tree09eb0b79da948c70ad92c231bb7438b548e6355c /numpy/random
parent672fd3f5677f70b118833dd38b9ed5a9d7c9268c (diff)
downloadnumpy-dccd8d0d688d479dfe26361eb3f18745fe690c10.tar.gz
BUG, STY: Make gaussian random number generators with identical behaviour
have identical pickles.
Diffstat (limited to 'numpy/random')
-rw-r--r--numpy/random/mtrand/initarray.c1
-rw-r--r--numpy/random/mtrand/randomkit.c32
2 files changed, 19 insertions, 14 deletions
diff --git a/numpy/random/mtrand/initarray.c b/numpy/random/mtrand/initarray.c
index 9a559c045..d36d512e3 100644
--- a/numpy/random/mtrand/initarray.c
+++ b/numpy/random/mtrand/initarray.c
@@ -145,6 +145,7 @@ init_by_array(rk_state *self, unsigned long init_key[], unsigned long key_length
}
mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
+ self->gauss = 0;
self->has_gauss = 0;
self->has_binomial = 0;
}
diff --git a/numpy/random/mtrand/randomkit.c b/numpy/random/mtrand/randomkit.c
index 915ce635c..b18897e2c 100644
--- a/numpy/random/mtrand/randomkit.c
+++ b/numpy/random/mtrand/randomkit.c
@@ -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 @@ static unsigned long rk_hash(unsigned long key);
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 @@ rk_randomseed(rk_state *state)
/* 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 @@ double
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 @@ rk_gauss(rk_state *state)
/* 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;
}
}