improve random number generation

This commit is contained in:
h_vogt 2010-09-01 21:13:01 +00:00
parent a4d4e91ab4
commit 453b565f71
8 changed files with 42 additions and 58 deletions

View File

@ -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

View File

@ -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

View File

@ -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;

View File

@ -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

View File

@ -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 --- */

View File

@ -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);
}

View File

@ -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); */

View File

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