Commit c30d8222 authored by deuce's avatar deuce
Browse files

Actually define HAS_DEV_RANDOM and RANDOM_DEV as well as HAS_DEV_URANDOM

and URANDOM_DEV used for seeding.
Fall-through seeding if there are problems opening URANDOM_DEV.
Use random() and srandom() where available as it's considered quite a bit
better than rand().
Define (but don't use) HAS_SRANDOMDEV_FUNC to indicate that srandomdev()
is avaiable.  The reason it's not used is that xp_randomize() returns the
seed value presumably to make it possible to replay a random sequence and
srandomdev() creates states which cannot be replicated on demand.
parent 293a9a86
......@@ -22,3 +22,37 @@ endif
ifeq ($(os),linux) # Linux
XPDEV-MT_LIBS += -lpthread
endif
ifeq ($(shell if [ -c /dev/urandom ] ; then echo YES ; fi),YES)
XPDEV-MT_CFLAGS += -DHAS_DEV_URANDOM -DURANDOM_DEV=\"/dev/urandom\"
XPDEV_CFLAGS += -DHAS_DEV_URANDOM -DURANDOM_DEV=\"/dev/urandom\"
endif
ifeq ($(shell if [ -c /dev/random ] ; then echo YES ; fi),YES)
XPDEV-MT_CFLAGS += -DHAS_DEV_RANDOM -DRANDOM_DEV=\"/dev/random\"
XPDEV_CFLAGS += -DHAS_DEV_RANDOM -DRANDOM_DEV=\"/dev/random\"
endif
# FreeBSD, OpenBSD, NetBSD, Linux, SunOS, and Darwin all have random()/srandom()
ifeq ($(os),freebsd)
XPDEV-MT_CFLAGS += -DHAS_RANDOM_FUNC -DHAS_SRANDOMDEV_FUNC
XPDEV_CFLAGS += -DHAS_RANDOM_FUNC -DHAS_SRANDOMDEV_FUNC
endif
ifeq ($(os),openbsd)
XPDEV-MT_CFLAGS += -DHAS_RANDOM_FUNC -DHAS_SRANDOMDEV_FUNC
XPDEV_CFLAGS += -DHAS_RANDOM_FUNC -DHAS_SRANDOMDEV_FUNC
endif
ifeq ($(os),netbsd)
XPDEV-MT_CFLAGS += -DHAS_RANDOM_FUNC
XPDEV_CFLAGS += -DHAS_RANDOM_FUNC
endif
ifeq ($(os),linux)
XPDEV-MT_CFLAGS += -DHAS_RANDOM_FUNC
XPDEV_CFLAGS += -DHAS_RANDOM_FUNC
endif
ifeq ($(os),sunos)
XPDEV-MT_CFLAGS += -DHAS_RANDOM_FUNC
XPDEV_CFLAGS += -DHAS_RANDOM_FUNC
endif
ifeq ($(os),darwin)
XPDEV-MT_CFLAGS += -DHAS_RANDOM_FUNC
XPDEV_CFLAGS += -DHAS_RANDOM_FUNC
endif
......@@ -238,27 +238,35 @@ unsigned DLLCALL xp_randomize(void)
{
unsigned seed=~0;
#if defined(HAS_DEV_RANDOM) && defined(RANDOM_DEV)
#if defined(HAS_DEV_URANDOM) && defined(URANDOM_DEV)
int rf;
if((rf=open(RANDOM_DEV, O_RDONLY))!=-1) {
if((rf=open(URANDOM_DEV, O_RDONLY))!=-1) {
read(rf, &seed, sizeof(seed));
close(rf);
}
#else
unsigned curtime = (unsigned)time(NULL);
unsigned process_id = (unsigned)GetCurrentProcessId();
else {
#endif
unsigned curtime = (unsigned)time(NULL);
unsigned process_id = (unsigned)GetCurrentProcessId();
seed = curtime ^ BYTE_SWAP_INT(process_id);
seed = curtime ^ BYTE_SWAP_INT(process_id);
#if defined(_WIN32) || defined(GetCurrentThreadId)
seed ^= (unsigned)GetCurrentThreadId();
#endif
#if defined(_WIN32) || defined(GetCurrentThreadId)
seed ^= (unsigned)GetCurrentThreadId();
#endif
#if defined(HAS_DEV_URANDOM) && defined(URANDOM_DEV)
}
#endif
#ifdef HAS_RANDOM_FUNC
srandom(seed);
return(seed);
#else
srand(seed);
return(seed);
#endif
}
/****************************************************************************/
......@@ -266,6 +274,9 @@ unsigned DLLCALL xp_randomize(void)
/****************************************************************************/
int DLLCALL xp_random(int n)
{
#ifdef HAS_RANDOM_FUNC
return(random()%n);
#else
float f;
if(n<2)
......@@ -273,6 +284,7 @@ int DLLCALL xp_random(int n)
f=(float)rand()/(float)RAND_MAX;
return((int)(n*f));
#endif
}
/****************************************************************************/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment