]> git.ipfire.org Git - thirdparty/make.git/commitdiff
[SV 63215] Remember the random seed for shuffle mode
authorPaul Smith <psmith@gnu.org>
Sat, 15 Oct 2022 22:11:21 +0000 (18:11 -0400)
committerPaul Smith <psmith@gnu.org>
Sun, 16 Oct 2022 20:18:21 +0000 (16:18 -0400)
Basic fix provided by James Hilliard <james.hilliard1@gmail.com>.
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.

src/main.c
src/misc.c
src/shuffle.c

index 05b11c6388d8df1aaad65539af625dfe6426e38d..d9068ae0cce04e23473a2109182643decb2efc52 100644 (file)
@@ -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.  */
index 011e4f225e74f37f1e04b5466f500eed323a7990..264eeb6ab8398cb9b935505757766f8a53d1400b 100644 (file)
@@ -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;
index 4cfdc943a35e452ac372dcf2a24c2b2acb58805c..17731e8f78f0fc413f511289c008e5e11b626aa0 100644 (file)
@@ -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;