From: Paul Smith Date: Sat, 15 Oct 2022 22:11:21 +0000 (-0400) Subject: [SV 63215] Remember the random seed for shuffle mode X-Git-Tag: 4.3.91~11 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e5e538fb7a9cb6a42dae6aa1ad9f36a3dc769ad9;p=thirdparty%2Fmake.git [SV 63215] Remember the random seed for shuffle mode Basic fix provided by James Hilliard . Ensure we remember and propagate the random seed we generate during shuffle mode. Also add a debug statement displaying the seed. * src/shuffle.c (shuffle_set_mode): Init and save the randoms seed. * src/misc.c (make_rand): Code cleanups. * src/main.c (main): Show a debug message containing the seed. --- diff --git a/src/main.c b/src/main.c index 05b11c63..d9068ae0 100644 --- a/src/main.c +++ b/src/main.c @@ -2318,6 +2318,9 @@ main (int argc, char **argv, char **envp) OUTPUT_UNSET (); output_close (&make_sync); + if (shuffle_mode) + DB (DB_BASIC, (_("Enabled shuffle mode: %s\n"), shuffle_mode)); + if (read_files) { /* Update any makefiles if necessary. */ diff --git a/src/misc.c b/src/misc.c index 011e4f22..264eeb6a 100644 --- a/src/misc.c +++ b/src/misc.c @@ -75,23 +75,22 @@ make_ulltoa (unsigned long long val, char *buf) /* Simple random number generator, for use with shuffle. This doesn't need to be truly random, just pretty random. Use our own implementation rather than relying on the C runtime's rand() so we always - get the same results for a given seed, regardless of OS. */ + get the same results for a given seed, regardless of C runtime. */ static unsigned int mk_state = 0; void -make_seed(unsigned int seed) +make_seed (unsigned int seed) { mk_state = seed; } unsigned int -make_rand() +make_rand () { /* mk_state must never be 0. */ - if (mk_state == 0) { + if (mk_state == 0) mk_state = (unsigned int)(time (NULL) ^ make_pid ()) + 1; - } /* A simple xorshift RNG. */ mk_state ^= mk_state << 13; diff --git a/src/shuffle.c b/src/shuffle.c index 4cfdc943..17731e8f 100644 --- a/src/shuffle.c +++ b/src/shuffle.c @@ -82,7 +82,9 @@ shuffle_set_mode (const char *cmdarg) } else { - if (strcasecmp (cmdarg, "random") != 0) + if (strcasecmp (cmdarg, "random") == 0) + config.seed = make_rand (); + else { /* Assume explicit seed. */ const char *err;