From 33832ea317f8fc57c251e0dce04b67760a378bac Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Tue, 5 Aug 2014 07:19:34 -0700 Subject: * rand.c (make_random_state): Make the seeding behavior portable when the seed is a fixnum or bignum. The goal is that the same values of seed should produce the same random sequences on any platform. --- ChangeLog | 7 +++++++ rand.c | 20 +++++++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index 42bbbaac..cd9503ef 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2014-08-05 Kaz Kylheku + + * rand.c (make_random_state): Make the seeding behavior + portable when the seed is a fixnum or bignum. The goal is that the same + values of seed should produce the same random sequences on any + platform. + 2014-08-05 Kaz Kylheku * eval.c (eval_init): Fix incorrect registration of diff --git a/rand.c b/rand.c index 5e583f76..3ebc6842 100644 --- a/rand.c +++ b/rand.c @@ -125,19 +125,25 @@ val make_random_state(val seed) dig++, bit = 0; } - for (; i < 16; i++) { - r->state[i] = 0xAAAAAAAA; - } + while (i > 0 && !r->state[i - 1]) + i--; + + if (i < 16) + memset(r->state + i, 0xAA, sizeof r->state - i * sizeof r->state[0]); } else if (fixnump(seed)) { cnum s = c_num(seed) & NUM_MAX; memset(r->state, 0xAA, sizeof r->state); r->state[0] = s & 0xFFFFFFFFul; #if SIZEOF_PTR >= 8 - r->state[1] = (s >> 32) & 0xFFFFFFFFul; -#elif SIZEOF_PTR >= 16 - r->state[2] = (s >> 64) & 0xFFFFFFFFul; - r->state[3] = (s >> 96) & 0xFFFFFFFFul; + s >>= 32; + if (s) + r->state[1] = s & 0xFFFFFFFFul; +#endif +#if SIZEOF_PTR >= 16 + s >>= 32; + if (s) + r->state[2] = s & 0xFFFFFFFFul; #endif } else if (nilp(seed)) { val time = time_sec_usec(); -- cgit v1.2.3