diff options
| author | Gareth Rees | 2013-05-16 13:17:42 +0100 |
|---|---|---|
| committer | Gareth Rees | 2013-05-16 13:17:42 +0100 |
| commit | d4fbeed6f7b17abfbe4d33260e65d7e7fd974933 (patch) | |
| tree | 79c2b0bdb24e8aba95a041c75b754749056186bb /mps/code/testlib.c | |
| parent | a2f7f16444e2a38e46e868451a8b571c9fe830b7 (diff) | |
| download | emacs-d4fbeed6f7b17abfbe4d33260e65d7e7fd974933.tar.gz emacs-d4fbeed6f7b17abfbe4d33260e65d7e7fd974933.zip | |
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
Diffstat (limited to 'mps/code/testlib.c')
| -rw-r--r-- | mps/code/testlib.c | 39 |
1 files changed, 10 insertions, 29 deletions
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 @@ | |||
| 8 | */ | 8 | */ |
| 9 | 9 | ||
| 10 | #include "testlib.h" | 10 | #include "testlib.h" |
| 11 | #include "clock.h" /* for EVENT_CLOCK */ | ||
| 11 | #include "mps.h" | 12 | #include "mps.h" |
| 12 | #include "misc.h" /* for NOOP */ | 13 | #include "misc.h" /* for NOOP */ |
| 13 | #include <math.h> | 14 | #include <math.h> |
| @@ -246,17 +247,15 @@ mps_addr_t rnd_addr(void) | |||
| 246 | * that's impossible. | 247 | * that's impossible. |
| 247 | * (2008..2010-03-22) | 248 | * (2008..2010-03-22) |
| 248 | * | 249 | * |
| 249 | * 3. v3 states: when autogenerated from time(), the published state | 250 | * 3. v3 states: the published state is the state *after* all |
| 250 | * (printf'd) is that *after* the 10 rnds. Therefore you can get | 251 | * initialization is complete. Therefore you can easily store and |
| 251 | * the state easily, store it, re-use it, etc. | 252 | * re-use the published state. (From 2010-03-22, changelist |
| 252 | * (New from 2010-03-22, changelist 170093) | 253 | * 170093). |
| 253 | */ | 254 | */ |
| 254 | 255 | ||
| 255 | void randomize(int argc, char *argv[]) | 256 | void randomize(int argc, char *argv[]) |
| 256 | { | 257 | { |
| 257 | int i; | ||
| 258 | int n; | 258 | int n; |
| 259 | unsigned long seedt; | ||
| 260 | unsigned long seed0; | 259 | unsigned long seed0; |
| 261 | 260 | ||
| 262 | if (argc > 1) { | 261 | if (argc > 1) { |
| @@ -266,29 +265,11 @@ void randomize(int argc, char *argv[]) | |||
| 266 | argv[0], seed0); | 265 | argv[0], seed0); |
| 267 | rnd_state_set(seed0); | 266 | rnd_state_set(seed0); |
| 268 | } else { | 267 | } else { |
| 269 | /* time_t uses an arbitrary encoding, but hopefully the low order */ | 268 | /* Initialize seed based on seconds since epoch and on processor |
| 270 | /* 31 bits will have at least one bit changed from run to run. */ | 269 | * cycle count. */ |
| 271 | seedt = 1 + (unsigned long)time(NULL) % (R_m - 1); | 270 | EventClock t2; |
| 272 | 271 | EVENT_CLOCK(t2); | |
| 273 | /* The value returned by time() on some OSs may simply be a | 272 | seed0 = 1 + ((unsigned long)time(NULL) + (unsigned long)t2) % (R_m - 1); |
| 274 | * count of seconds: therefore successive runs may start with | ||
| 275 | * nearby seeds, possibly differing only by 1. So the first value | ||
| 276 | * returned by rnd() may differ by only 48271. It is conceivable | ||
| 277 | * that some tests might be able to 'spot' this pattern (for | ||
| 278 | * example: by using the first rnd() value, mod 100M and rounded | ||
| 279 | * to multiple of 1024K, as arena size in bytes). | ||
| 280 | * | ||
| 281 | * So to mix it up a bit, we do a few iterations now. How many? | ||
| 282 | * Very roughly, 48271^2 is of the same order as 2^31, so two | ||
| 283 | * iterations would make the characteristic difference similar to | ||
| 284 | * the period. Hey, let's go wild and do 10. | ||
| 285 | */ | ||
| 286 | rnd_state_set(seedt); | ||
| 287 | for(i = 0; i < 10; i += 1) { | ||
| 288 | (void)rnd(); | ||
| 289 | } | ||
| 290 | |||
| 291 | seed0 = rnd_state(); | ||
| 292 | printf("%s: randomize(): choosing initial state (v3): %lu.\n", | 273 | printf("%s: randomize(): choosing initial state (v3): %lu.\n", |
| 293 | argv[0], seed0); | 274 | argv[0], seed0); |
| 294 | rnd_state_set(seed0); | 275 | rnd_state_set(seed0); |