From 7a1152369227e81f4ec1c14c2c090fb02950b075 Mon Sep 17 00:00:00 2001 From: david Date: Wed, 31 Jul 2002 17:14:29 +0000 Subject: [PATCH] Random-number seed patch from Erik Hofman. --- configure.in | 2 +- simgear/math/sg_random.c | 33 ++++++++++++++++----------------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/configure.in b/configure.in index c2e2a326..aa443d51 100644 --- a/configure.in +++ b/configure.in @@ -322,7 +322,7 @@ dnl Checks for library functions. AC_TYPE_SIGNAL AC_FUNC_VPRINTF AC_CHECK_FUNCS( ftime gettimeofday timegm memcpy bcopy mktime strstr rand \ - random setitimer getitimer signal GetLocalTime rint getrusage ) + random drand48 setitimer getitimer signal GetLocalTime rint getrusage ) AM_CONFIG_HEADER(simgear/simgear_config.h) diff --git a/simgear/math/sg_random.c b/simgear/math/sg_random.c index f67f1341..ec103f13 100644 --- a/simgear/math/sg_random.c +++ b/simgear/math/sg_random.c @@ -27,18 +27,11 @@ #endif #include -#include // for random(), srandom() +#include // for random(), srandom(), drand48() #include // for time() to seed srandom() #include "sg_random.h" -#ifndef HAVE_RAND -# ifdef sgi -# undef RAND_MAX -# define RAND_MAX 2147483647 -# endif -#endif - #ifdef __SUNPRO_CC extern "C" { long int random(); @@ -50,10 +43,12 @@ // Seed the random number generater with time() so we don't see the // same sequence every time void sg_srandom_time() { -#ifdef HAVE_RAND - srand(time(NULL)); -#else +#ifdef HAVE_DRAND48 + srand48(time(NULL) + getpid()); +#elif defined( HAVE_RANDOM ) srandom(time(NULL)); +#else + srand(time(NULL)); #endif } @@ -61,20 +56,24 @@ void sg_srandom_time() { // Seed the random number generater with your own seed so can set up // repeatable randomization. void sg_srandom( unsigned int seed ) { -#ifdef HAVE_RAND - srand( seed ); -#else +#ifdef HAVE_DRAND48 + srand48( seed ); +#elif defined( HAVE_RANDOM ) srandom( seed ); +#else + srand( seed ); #endif } // return a random number between [0.0, 1.0) double sg_random() { -#ifdef HAVE_RAND - return(rand() / (double)RAND_MAX); -#else +#ifdef HAVE_DRAND48 + return(drand48()); +#elif defined( HAVE_RANDOM ) return(random() / (double)RAND_MAX); +#else + return(rand() / (double)RAND_MAX); #endif } -- 2.39.5