From: Eric S. Raymond Date: Thu, 25 May 2017 03:36:25 +0000 (-0400) Subject: Once again, take srand()/random() out of the initialization chain. X-Git-Tag: seed~53 X-Git-Url: https://jxself.org/git/?a=commitdiff_plain;h=6ba9a7de7fe7c829a2215f254bfbbbd2a5686584;p=open-adventure.git Once again, take srand()/random() out of the initialization chain. They have exactly the wrong kind of randomness for this job - not returning consistent sequences across different platforms or C library versions, and because pseodorandom not really better than sampling the clock. --- diff --git a/main.c b/main.c index 6cfc411..ba7df1a 100644 --- a/main.c +++ b/main.c @@ -99,7 +99,7 @@ int main(int argc, char *argv[]) { lcgstate.a = 1093; lcgstate.c = 221587; lcgstate.m = 1048576; - set_seed_from_time(); + set_seed((long)time(NULL)); /* Read the database if we have not yet done so */ diff --git a/misc.c b/misc.c index 7057bad..b6ca80e 100644 --- a/misc.c +++ b/misc.c @@ -724,7 +724,8 @@ L2: ATDWRF=I; -/* Utility routines (SETBIT, TSTBIT, set_seed_from_time, get_next_lcg_value, randrange, RNDVOC, BUG) */ +/* Utility routines (SETBIT, TSTBIT, set_seed, get_next_lcg_value, + * randrange, RNDVOC, BUG) */ #undef SETBIT long fSETBIT(long BIT) { @@ -759,11 +760,9 @@ long TSTBIT; #define TSTBIT(MASK,BIT) fTSTBIT(MASK,BIT) -void set_seed_from_time(void) +void set_seed(long seedval) { - /* Use the current system time to get seed the ISO rand() function, from which we get a seed for the LCG. */ - srand(time(NULL)); - lcgstate.x = (unsigned long) rand() % lcgstate.m; + lcgstate.x = (unsigned long) seedval % lcgstate.m; } unsigned long get_next_lcg_value(void) diff --git a/misc.h b/misc.h index 095efba..f4567e3 100644 --- a/misc.h +++ b/misc.h @@ -73,6 +73,6 @@ extern long fIABS(long); #define IABS(N) fIABS(N) extern long fMOD(long,long); #define MOD(N,M) fMOD(N,M) -extern void set_seed_from_time(void); +extern void set_seed(long); extern unsigned long get_next_lcg_value(void); extern long randrange(long);