Use more portable random seed generation.

The new function md_random_seed() has replaced time() + getpid() and
similar methods.  Putting everything in mdport.c slightly reduces the
warnings and workarounds.
This commit is contained in:
John "Elwin" Edwards 2017-01-28 15:49:41 -05:00
parent c194ec3dc9
commit 62047972cc
29 changed files with 123 additions and 81 deletions

View file

@ -93,3 +93,4 @@ extern int md_shellescape(void);
extern void md_sleep(int s);
extern int md_unlink(char *file);
extern int md_unlink_open_file(char *file, int inf);
extern unsigned int md_random_seed(void);

View file

@ -29,7 +29,6 @@ int
main(int argc, char *argv[], char *envp[])
{
register char *env;
int lowtime;
md_init();
@ -138,8 +137,6 @@ main(int argc, char *argv[], char *envp[])
if (!use_savedir)
md_normaluser();
lowtime = (int) time(NULL);
#ifdef WIZARD
noscore = wizard;
#endif
@ -149,7 +146,7 @@ main(int argc, char *argv[], char *envp[])
noscore = TRUE;
}
else
dnum = lowtime + getpid();
dnum = md_random_seed();
#ifdef WIZARD
if (wizard)
printf("Hello %s, welcome to dungeon #%d", whoami, dnum);

View file

@ -82,6 +82,7 @@ char *strdup(const char *s);
#include <limits.h>
#include <sys/stat.h>
#include <signal.h>
#include <time.h>
#define MOD_MOVE(c) (toupper(c) )
@ -1313,3 +1314,16 @@ md_readchar(WINDOW *win)
return(ch & 0x7F);
}
unsigned int
md_random_seed(void)
{
unsigned int seed;
seed = (unsigned int) time((time_t *) NULL);
#ifdef _WIN32
seed += _getpid();
#else
seed += getpid();
#endif
return seed;
}

View file

@ -73,7 +73,7 @@ new_level(void)
if (i++ > 100)
{
i = 0;
srand(getpid() + (int) time((time_t *) NULL));
srand(md_random_seed());
}
} until (_level[index] == FLOOR);
_level[index] = STAIRS;

View file

@ -317,7 +317,7 @@ restore(char *file, char **envp)
setup();
clearok(curscr, TRUE);
touchwin(stdscr);
srand(getpid());
srand(md_random_seed());
msg("file name: %s", file);
status();
playit();