Browse Source

improve random number generation

pre-master-46
h_vogt 16 years ago
parent
commit
453b565f71
  1. 6
      ChangeLog
  2. 3
      configure.in
  3. 4
      src/frontend/numparam/mystring.c
  4. 3
      src/include/ngspice.h
  5. 2
      src/main.c
  6. 10
      src/maths/cmaths/cmath2.c
  7. 66
      src/maths/misc/randnumb.c
  8. 6
      src/tclspice.c

6
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 2010-08-29 Dietmar Warning
* fteext.h, parse.c, cmath2.c, cmath2.h,: * fteext.h, parse.c, cmath2.c, cmath2.h,:
new function sunif(), uniform random generator usable in control blocks new function sunif(), uniform random generator usable in control blocks

3
configure.in

@ -662,9 +662,6 @@ if test "$ac_cv_have_decl_isnan" != yes; then
AC_CHECK_FUNC(isnan) AC_CHECK_FUNC(isnan)
fi 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 dnl If user enables garbage collection, look for garbage collector
if test "$TCL_PACKAGE_PATH" = ""; then if test "$TCL_PACKAGE_PATH" = ""; then
if test "$enable_gc" = "yes"; then if test "$enable_gc" = "yes"; then

4
src/frontend/numparam/mystring.c

@ -1042,7 +1042,9 @@ np_round (double x)
double u; double u;
long z; long z;
int n; int n;
Str (40, s);
// Str (40, s);
SPICE_DSTRING s ;
spice_dstring_init(&s) ;
u = 2e9; u = 2e9;
if (x > u) if (x > u)
x = u; x = u;

3
src/include/ngspice.h

@ -175,6 +175,7 @@ extern struct timeb timebegin;
#define inline _inline #define inline _inline
#endif #endif
/*
#ifndef HAVE_RANDOM #ifndef HAVE_RANDOM
#define srandom(a) srand(a) #define srandom(a) srand(a)
#define random rand #define random rand
@ -182,6 +183,8 @@ extern struct timeb timebegin;
#else #else
#define RR_MAX LONG_MAX #define RR_MAX LONG_MAX
#endif #endif
*/
#define RR_MAX RAND_MAX
#ifdef HAVE_INDEX #ifdef HAVE_INDEX
# define strchr index # define strchr index

2
src/main.c

@ -787,7 +787,7 @@ main(int argc, char **argv)
} }
cp_program = ft_sim->simulator; cp_program = ft_sim->simulator;
srandom(getpid());
srand(getpid()); //srandom(getpid());
TausSeed(); TausSeed();
/* --- Process command line options --- */ /* --- Process command line options --- */

10
src/maths/cmaths/cmath2.c

@ -24,12 +24,12 @@ Author: 1985 Wayne A. Christopher, U. C. Berkeley CAD Group
#include "cmath2.h" #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) #if defined(__MINGW32__) && defined(HAVE_RANDOM)
extern long int random (void); extern long int random (void);
extern void srandom (unsigned int seed); extern void srandom (unsigned int seed);
#endif #endif
*/
extern void checkseed(void); /* seed random or set by 'set rndseed=value'*/ extern void checkseed(void); /* seed random or set by 'set rndseed=value'*/
extern double drand(void); /* from randnumb.c */ extern double drand(void); /* from randnumb.c */
extern double gauss(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; int j, k;
j = (int)floor(realpart(&cc[i])); j = (int)floor(realpart(&cc[i]));
k = (int)floor(imagpart(&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); return ((void *) c);
} else { } else {
@ -240,7 +240,7 @@ cx_rnd(void *data, short int type, int length, int *newlength, short int *newtyp
int j; int j;
j = (int)floor(dd[i]); j = (int)floor(dd[i]);
d[i] = j ? random() % j : 0;
d[i] = j ? rand() % j : 0; //random() % j : 0;
} }
return ((void *) d); return ((void *) d);
} }

66
src/maths/misc/randnumb.c

@ -54,23 +54,18 @@ Copyright 2008 Holger Vogt
/* Tausworthe state variables for double variates*/ /* Tausworthe state variables for double variates*/
static unsigned CombState1 = 129, CombState2 = 130, CombState3 = 131; 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 CombState5 = 133, CombState6 = 135, CombState7 = 137;
static unsigned CombState8; /* LCG state variable */
unsigned TauS(unsigned *state, int C1, int C2, int C3, unsigned m);
unsigned LGCS(unsigned *state, unsigned A1, unsigned A2);
void TausSeed(void);
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);
/* 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
void TausSeed(void);
double CombLCGTaus(void);
unsigned int CombLCGTausInt(void);
void checkseed(void); void checkseed(void);
double drand(void); double drand(void);
@ -89,7 +84,7 @@ void checkseed(void)
/* printf("Enter checkseed()\n"); */ /* printf("Enter checkseed()\n"); */
if (cp_getvar("rndseed", CP_NUM, &newseed)) { if (cp_getvar("rndseed", CP_NUM, &newseed)) {
if ((newseed > 0) && (oldseed != newseed)) { if ((newseed > 0) && (oldseed != newseed)) {
srandom(newseed);
srand(newseed); //srandom(newseed);
TausSeed(); TausSeed();
oldseed = newseed; oldseed = newseed;
printf("Seed value for random number generator is set to %d\n", newseed); printf("Seed value for random number generator is set to %d\n", newseed);
@ -103,50 +98,39 @@ void checkseed(void)
double drand(void) double drand(void)
{ {
checkseed(); 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) 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 #ifdef HVDEBUG
printf("\nTausworthe Double generator init states: %d, %d, %d, %d\n", printf("\nTausworthe Double generator init states: %d, %d, %d, %d\n",
CombState1, CombState2, CombState3, CombState4); 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", printf("Tausworthe Integer generator init states: %d, %d, %d, %d\n",
CombState5, CombState6, CombState7, CombState8); CombState5, CombState6, CombState7, CombState8);
#endif #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); unsigned b = (((*state << C1) ^ *state) >> C2);
return *state = (((*state & m) << C3) ^ b); 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); return *state = (A1 * *state + A2);
} }
@ -211,8 +195,6 @@ double gauss(void)
if (gliset) { if (gliset) {
do { do {
v1 = drand(); v2 = drand(); v1 = drand(); v2 = drand();
// v1 = 2.0 * CombLCGTaus() - 1.0;
// v2 = 2.0 * CombLCGTaus() - 1.0;
r = v1*v1 + v2*v2; r = v1*v1 + v2*v2;
} while (r >= 1.0); } while (r >= 1.0);
/* printf("v1 %f, v2 %f\n", v1, v2); */ /* printf("v1 %f, v2 %f\n", v1, v2); */

6
src/tclspice.c

@ -66,17 +66,11 @@ typedef pthread_t threadId_t;
#undef BOOLEAN #undef BOOLEAN
#include <windef.h> #include <windef.h>
#include <winbase.h> /* Sleep */ #include <winbase.h> /* Sleep */
#ifndef srandom
#define srandom(a) srand(a) /* srandom */
#endif
#elif defined(_MSC_VER) #elif defined(_MSC_VER)
#include <stdarg.h> #include <stdarg.h>
/* remove type incompatibility with winnt.h*/ /* remove type incompatibility with winnt.h*/
#undef BOOLEAN #undef BOOLEAN
#include <windows.h> /* Sleep */ #include <windows.h> /* Sleep */
#ifndef srandom
#define srandom(a) srand(a) /* srandom */
#endif
#include <process.h> /* _getpid */ #include <process.h> /* _getpid */
#define dup _dup #define dup _dup
#define dup2 _dup2 #define dup2 _dup2

Loading…
Cancel
Save