From 453b565f71b801cf0b23650f558891d2cb3fd064 Mon Sep 17 00:00:00 2001 From: h_vogt Date: Wed, 1 Sep 2010 21:13:01 +0000 Subject: [PATCH] improve random number generation --- ChangeLog | 6 +++ configure.in | 3 -- src/frontend/numparam/mystring.c | 4 +- src/include/ngspice.h | 3 ++ src/main.c | 2 +- src/maths/cmaths/cmath2.c | 10 ++--- src/maths/misc/randnumb.c | 66 ++++++++++++-------------------- src/tclspice.c | 6 --- 8 files changed, 42 insertions(+), 58 deletions(-) diff --git a/ChangeLog b/ChangeLog index d562f26a2..b26317819 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2010-08-29 Holger Vogt + * cmath2.c, randnumb.c, main.c, ngspice.h, configure.in, tclspice.c: + remove fcns random() and srandom(), only use rand() and srand() or + internal random number generator. + * mystring.c: replace Str() by SPICE_DSTRING + 2010-08-29 Dietmar Warning * fteext.h, parse.c, cmath2.c, cmath2.h,: new function sunif(), uniform random generator usable in control blocks diff --git a/configure.in b/configure.in index 6e163fbfb..a267f9fc4 100644 --- a/configure.in +++ b/configure.in @@ -662,9 +662,6 @@ if test "$ac_cv_have_decl_isnan" != yes; then AC_CHECK_FUNC(isnan) fi -dnl Check for the random function: -AC_CHECK_FUNCS(random,,AC_CHECK_LIB(iberty,random,AC_DEFINE([HAVE_RANDOM],1,[Have random in libiberty]) LIBS="$LIBS -liberty")) - dnl If user enables garbage collection, look for garbage collector if test "$TCL_PACKAGE_PATH" = ""; then if test "$enable_gc" = "yes"; then diff --git a/src/frontend/numparam/mystring.c b/src/frontend/numparam/mystring.c index 9c0779381..8e1c839e0 100644 --- a/src/frontend/numparam/mystring.c +++ b/src/frontend/numparam/mystring.c @@ -1042,7 +1042,9 @@ np_round (double x) double u; long z; int n; - Str (40, s); +// Str (40, s); + SPICE_DSTRING s ; + spice_dstring_init(&s) ; u = 2e9; if (x > u) x = u; diff --git a/src/include/ngspice.h b/src/include/ngspice.h index 6dd76ef18..ee72886dd 100644 --- a/src/include/ngspice.h +++ b/src/include/ngspice.h @@ -175,6 +175,7 @@ extern struct timeb timebegin; #define inline _inline #endif +/* #ifndef HAVE_RANDOM #define srandom(a) srand(a) #define random rand @@ -182,6 +183,8 @@ extern struct timeb timebegin; #else #define RR_MAX LONG_MAX #endif +*/ +#define RR_MAX RAND_MAX #ifdef HAVE_INDEX # define strchr index diff --git a/src/main.c b/src/main.c index 1d696bc2d..b9c06f665 100644 --- a/src/main.c +++ b/src/main.c @@ -787,7 +787,7 @@ main(int argc, char **argv) } cp_program = ft_sim->simulator; - srandom(getpid()); + srand(getpid()); //srandom(getpid()); TausSeed(); /* --- Process command line options --- */ diff --git a/src/maths/cmaths/cmath2.c b/src/maths/cmaths/cmath2.c index 3104747fd..62adbb43b 100644 --- a/src/maths/cmaths/cmath2.c +++ b/src/maths/cmaths/cmath2.c @@ -24,12 +24,12 @@ Author: 1985 Wayne A. Christopher, U. C. Berkeley CAD Group #include "cmath2.h" -/* MINGW: random, srandom in libiberty.a, but not in libiberty.h */ +/* MINGW: random, srandom in libiberty.a, but not in libiberty.h #if defined(__MINGW32__) && defined(HAVE_RANDOM) extern long int random (void); extern void srandom (unsigned int seed); #endif - +*/ extern void checkseed(void); /* seed random or set by 'set rndseed=value'*/ extern double drand(void); /* from randnumb.c */ extern double gauss(void); /* from randnumb.c */ @@ -225,8 +225,8 @@ cx_rnd(void *data, short int type, int length, int *newlength, short int *newtyp int j, k; j = (int)floor(realpart(&cc[i])); k = (int)floor(imagpart(&cc[i])); - realpart(&c[i]) = j ? random() % j : 0; - imagpart(&c[i]) = k ? random() % k : 0; + realpart(&c[i]) = j ? rand() % j : 0; //random() % j : 0; + imagpart(&c[i]) = k ? rand() % k : 0; //random() % k : 0; } return ((void *) c); } else { @@ -240,7 +240,7 @@ cx_rnd(void *data, short int type, int length, int *newlength, short int *newtyp int j; j = (int)floor(dd[i]); - d[i] = j ? random() % j : 0; + d[i] = j ? rand() % j : 0; //random() % j : 0; } return ((void *) d); } diff --git a/src/maths/misc/randnumb.c b/src/maths/misc/randnumb.c index d7745a22e..7975c0412 100644 --- a/src/maths/misc/randnumb.c +++ b/src/maths/misc/randnumb.c @@ -54,23 +54,18 @@ Copyright 2008 Holger Vogt /* Tausworthe state variables for double variates*/ static unsigned CombState1 = 129, CombState2 = 130, CombState3 = 131; -static unsigned CombState4; /* LCG state variable */ +static unsigned CombState4 = 132; /* LCG state variable */ -/* Tausworthe state variables for double variates*/ +/* Tausworthe state variables for integer variates*/ static unsigned CombState5 = 133, CombState6 = 135, CombState7 = 137; -static unsigned CombState8; /* LCG state variable */ +static unsigned CombState8 = 138; /* LCG state variable */ + +static unsigned TauS(unsigned *state, int C1, int C2, int C3, unsigned m); +static unsigned LGCS(unsigned *state, unsigned A1, unsigned A2); -unsigned TauS(unsigned *state, int C1, int C2, int C3, unsigned m); -unsigned LGCS(unsigned *state, unsigned A1, unsigned A2); void TausSeed(void); - - - -/* MINGW: random, srandom in libiberty.a, but not in libiberty.h */ -#if defined(__MINGW32__) && defined(HAVE_RANDOM) -extern long int random (void); -extern void srandom (unsigned int seed); -#endif +double CombLCGTaus(void); +unsigned int CombLCGTausInt(void); void checkseed(void); double drand(void); @@ -89,7 +84,7 @@ void checkseed(void) /* printf("Enter checkseed()\n"); */ if (cp_getvar("rndseed", CP_NUM, &newseed)) { if ((newseed > 0) && (oldseed != newseed)) { - srandom(newseed); + srand(newseed); //srandom(newseed); TausSeed(); oldseed = newseed; printf("Seed value for random number generator is set to %d\n", newseed); @@ -103,50 +98,39 @@ void checkseed(void) double drand(void) { checkseed(); - return ( 2.0*((double) (RR_MAX-abs(random())) / (double)RR_MAX-0.5)); +// return ( 2.0*((double) (RR_MAX-abs(rand())) / (double)RR_MAX-0.5)); + return 2.0 * CombLCGTaus() - 1.0; } - void TausSeed(void) { - CombState1 = CombState2 = CombState3 = 0; - /* The Tausworthe initial states should be greater than 128 */ - while (CombState1 < 129) - CombState1 = rand(); - while (CombState2 < 129) - CombState2 = rand(); - while (CombState3 < 129) - CombState3 = rand(); - while (CombState4 < 129) - CombState4 = rand(); + /* The Tausworthe initial states should be greater than 128. + We restrict the values up to 32767 */ + CombState1 = (unsigned int)((double)rand()/(double)RR_MAX * 32638.) + 129; + CombState2 = (unsigned int)((double)rand()/(double)RR_MAX * 32638.) + 129; + CombState3 = (unsigned int)((double)rand()/(double)RR_MAX * 32638.) + 129; + CombState4 = (unsigned int)((double)rand()/(double)RR_MAX * 32638.) + 129; + CombState5 = (unsigned int)((double)rand()/(double)RR_MAX * 32638.) + 129; + CombState6 = (unsigned int)((double)rand()/(double)RR_MAX * 32638.) + 129; + CombState7 = (unsigned int)((double)rand()/(double)RR_MAX * 32638.) + 129; + CombState8 = (unsigned int)((double)rand()/(double)RR_MAX * 32638.) + 129; + #ifdef HVDEBUG printf("\nTausworthe Double generator init states: %d, %d, %d, %d\n", CombState1, CombState2, CombState3, CombState4); -#endif - CombState5 = CombState6 = CombState7 = 0; - /* The Tausworthe initial states should be greater than 128 */ - while (CombState5 < 129) - CombState5 = rand(); - while (CombState6 < 129) - CombState6 = rand(); - while (CombState7 < 129) - CombState7 = rand(); - while (CombState8 < 129) - CombState8 = rand(); -#ifdef HVDEBUG printf("Tausworthe Integer generator init states: %d, %d, %d, %d\n", CombState5, CombState6, CombState7, CombState8); #endif } -unsigned TauS(unsigned *state, int C1, int C2, int C3, unsigned m) +static unsigned TauS(unsigned *state, int C1, int C2, int C3, unsigned m) { unsigned b = (((*state << C1) ^ *state) >> C2); return *state = (((*state & m) << C3) ^ b); } -unsigned LGCS(unsigned *state, unsigned A1, unsigned A2) +static unsigned LGCS(unsigned *state, unsigned A1, unsigned A2) { return *state = (A1 * *state + A2); } @@ -211,8 +195,6 @@ double gauss(void) if (gliset) { do { v1 = drand(); v2 = drand(); -// v1 = 2.0 * CombLCGTaus() - 1.0; -// v2 = 2.0 * CombLCGTaus() - 1.0; r = v1*v1 + v2*v2; } while (r >= 1.0); /* printf("v1 %f, v2 %f\n", v1, v2); */ diff --git a/src/tclspice.c b/src/tclspice.c index 6496ef7c8..58ff33f0e 100755 --- a/src/tclspice.c +++ b/src/tclspice.c @@ -66,17 +66,11 @@ typedef pthread_t threadId_t; #undef BOOLEAN #include #include /* Sleep */ - #ifndef srandom - #define srandom(a) srand(a) /* srandom */ - #endif #elif defined(_MSC_VER) #include /* remove type incompatibility with winnt.h*/ #undef BOOLEAN #include /* Sleep */ - #ifndef srandom - #define srandom(a) srand(a) /* srandom */ - #endif #include /* _getpid */ #define dup _dup #define dup2 _dup2