From d4fbeed6f7b17abfbe4d33260e65d7e7fd974933 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Thu, 16 May 2013 13:17:42 +0100 Subject: Eventclock has enough entropy on all the platforms we support that we can use it to choose the random number seed. Copied from Perforce Change: 181855 ServerID: perforce.ravenbrook.com --- mps/code/testlib.c | 39 ++++++++++----------------------------- 1 file changed, 10 insertions(+), 29 deletions(-) (limited to 'mps/code') diff --git a/mps/code/testlib.c b/mps/code/testlib.c index be7284ea3fa..dae807b59fb 100644 --- a/mps/code/testlib.c +++ b/mps/code/testlib.c @@ -8,6 +8,7 @@ */ #include "testlib.h" +#include "clock.h" /* for EVENT_CLOCK */ #include "mps.h" #include "misc.h" /* for NOOP */ #include @@ -246,17 +247,15 @@ mps_addr_t rnd_addr(void) * that's impossible. * (2008..2010-03-22) * - * 3. v3 states: when autogenerated from time(), the published state - * (printf'd) is that *after* the 10 rnds. Therefore you can get - * the state easily, store it, re-use it, etc. - * (New from 2010-03-22, changelist 170093) + * 3. v3 states: the published state is the state *after* all + * initialization is complete. Therefore you can easily store and + * re-use the published state. (From 2010-03-22, changelist + * 170093). */ void randomize(int argc, char *argv[]) { - int i; int n; - unsigned long seedt; unsigned long seed0; if (argc > 1) { @@ -266,29 +265,11 @@ void randomize(int argc, char *argv[]) argv[0], seed0); rnd_state_set(seed0); } else { - /* time_t uses an arbitrary encoding, but hopefully the low order */ - /* 31 bits will have at least one bit changed from run to run. */ - seedt = 1 + (unsigned long)time(NULL) % (R_m - 1); - - /* The value returned by time() on some OSs may simply be a - * count of seconds: therefore successive runs may start with - * nearby seeds, possibly differing only by 1. So the first value - * returned by rnd() may differ by only 48271. It is conceivable - * that some tests might be able to 'spot' this pattern (for - * example: by using the first rnd() value, mod 100M and rounded - * to multiple of 1024K, as arena size in bytes). - * - * So to mix it up a bit, we do a few iterations now. How many? - * Very roughly, 48271^2 is of the same order as 2^31, so two - * iterations would make the characteristic difference similar to - * the period. Hey, let's go wild and do 10. - */ - rnd_state_set(seedt); - for(i = 0; i < 10; i += 1) { - (void)rnd(); - } - - seed0 = rnd_state(); + /* Initialize seed based on seconds since epoch and on processor + * cycle count. */ + EventClock t2; + EVENT_CLOCK(t2); + seed0 = 1 + ((unsigned long)time(NULL) + (unsigned long)t2) % (R_m - 1); printf("%s: randomize(): choosing initial state (v3): %lu.\n", argv[0], seed0); rnd_state_set(seed0); -- cgit v1.2.1