[SV 63215] Remember the random seed for shuffle mode

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.
This commit is contained in:
Paul Smith 2022-10-15 18:11:21 -04:00
parent c453f898a0
commit e5e538fb7a
3 changed files with 10 additions and 6 deletions

View File

@ -2318,6 +2318,9 @@ main (int argc, char **argv, char **envp)
OUTPUT_UNSET (); OUTPUT_UNSET ();
output_close (&make_sync); output_close (&make_sync);
if (shuffle_mode)
DB (DB_BASIC, (_("Enabled shuffle mode: %s\n"), shuffle_mode));
if (read_files) if (read_files)
{ {
/* Update any makefiles if necessary. */ /* Update any makefiles if necessary. */

View File

@ -75,23 +75,22 @@ make_ulltoa (unsigned long long val, char *buf)
/* Simple random number generator, for use with shuffle. /* Simple random number generator, for use with shuffle.
This doesn't need to be truly random, just pretty random. Use our own 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 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; static unsigned int mk_state = 0;
void void
make_seed(unsigned int seed) make_seed (unsigned int seed)
{ {
mk_state = seed; mk_state = seed;
} }
unsigned int unsigned int
make_rand() make_rand ()
{ {
/* mk_state must never be 0. */ /* mk_state must never be 0. */
if (mk_state == 0) { if (mk_state == 0)
mk_state = (unsigned int)(time (NULL) ^ make_pid ()) + 1; mk_state = (unsigned int)(time (NULL) ^ make_pid ()) + 1;
}
/* A simple xorshift RNG. */ /* A simple xorshift RNG. */
mk_state ^= mk_state << 13; mk_state ^= mk_state << 13;

View File

@ -82,7 +82,9 @@ shuffle_set_mode (const char *cmdarg)
} }
else else
{ {
if (strcasecmp (cmdarg, "random") != 0) if (strcasecmp (cmdarg, "random") == 0)
config.seed = make_rand ();
else
{ {
/* Assume explicit seed. */ /* Assume explicit seed. */
const char *err; const char *err;