update on random number usage
prefer gauss1() over gauss0()
This commit is contained in:
parent
f341003469
commit
1db4758ba8
|
|
@ -47,7 +47,7 @@ agauss(double nominal_val, double abs_variation, double sigma)
|
||||||
{
|
{
|
||||||
double stdvar;
|
double stdvar;
|
||||||
stdvar = abs_variation / sigma;
|
stdvar = abs_variation / sigma;
|
||||||
return (nominal_val + stdvar * gauss0());
|
return (nominal_val + stdvar * gauss1());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -56,7 +56,7 @@ gauss(double nominal_val, double rel_variation, double sigma)
|
||||||
{
|
{
|
||||||
double stdvar;
|
double stdvar;
|
||||||
stdvar = nominal_val * rel_variation / sigma;
|
stdvar = nominal_val * rel_variation / sigma;
|
||||||
return (nominal_val + stdvar * gauss0());
|
return (nominal_val + stdvar * gauss1());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
10
src/main.c
10
src/main.c
|
|
@ -1177,15 +1177,7 @@ main(int argc, char **argv)
|
||||||
fprintf(cp_out, "SoS %f, seed value: %ld\n", renormalize(), rseed);
|
fprintf(cp_out, "SoS %f, seed value: %ld\n", renormalize(), rseed);
|
||||||
}
|
}
|
||||||
#elif defined(WaGauss)
|
#elif defined(WaGauss)
|
||||||
{
|
initw();
|
||||||
unsigned int rseed = 66;
|
|
||||||
if (!cp_getvar("rndseed", CP_NUM, &rseed)) {
|
|
||||||
time_t acttime = time(NULL);
|
|
||||||
rseed = (unsigned int) acttime;
|
|
||||||
}
|
|
||||||
srand(rseed);
|
|
||||||
initw();
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (!ft_servermode) {
|
if (!ft_servermode) {
|
||||||
|
|
|
||||||
|
|
@ -305,7 +305,7 @@ cx_sgauss(void *data, short int type, int length, int *newlength, short int *new
|
||||||
d = alloc_d(length);
|
d = alloc_d(length);
|
||||||
*newtype = VF_REAL;
|
*newtype = VF_REAL;
|
||||||
for (i = 0; i < length; i++) {
|
for (i = 0; i < length; i++) {
|
||||||
d[i] = gauss0();
|
d[i] = gauss1();
|
||||||
}
|
}
|
||||||
return ((void *) d);
|
return ((void *) d);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -69,9 +69,10 @@ static bool seedinfo = FALSE;
|
||||||
|
|
||||||
|
|
||||||
/* Check if a seed has been set by the command 'set rndseed=value'
|
/* Check if a seed has been set by the command 'set rndseed=value'
|
||||||
in spinit with integer value > 0. If available, call srand(value).
|
in spinit, .spiceinit or in a .control section
|
||||||
This will override the call to srand in main.c.
|
with integer value > 0. If available, call srand(value).
|
||||||
Checkseed should be put in front of any call to rand or CombLCGTaus.. .
|
rndseed set in main.c to 1, if no 'set rndseed=val' is given.
|
||||||
|
Called from functions in cmath2.c.
|
||||||
*/
|
*/
|
||||||
void checkseed(void)
|
void checkseed(void)
|
||||||
{
|
{
|
||||||
|
|
@ -82,10 +83,10 @@ void checkseed(void)
|
||||||
if ((newseed > 0) && (oldseed != newseed)) {
|
if ((newseed > 0) && (oldseed != newseed)) {
|
||||||
srand((unsigned int)newseed);
|
srand((unsigned int)newseed);
|
||||||
TausSeed();
|
TausSeed();
|
||||||
|
if (oldseed > 0) /* no printout upon start-up */
|
||||||
|
printf("Seed value for random number generator is set to %d\n", newseed);
|
||||||
oldseed = newseed;
|
oldseed = newseed;
|
||||||
printf("Seed value for random number generator is set to %d\n", newseed);
|
|
||||||
}
|
}
|
||||||
/* else printf("Oldseed %d, newseed %d\n", oldseed, newseed); */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -93,8 +94,6 @@ void checkseed(void)
|
||||||
/* uniform random number generator, interval [-1 .. +1[ */
|
/* uniform random number generator, interval [-1 .. +1[ */
|
||||||
double drand(void)
|
double drand(void)
|
||||||
{
|
{
|
||||||
checkseed();
|
|
||||||
// return ( 2.0*((double) (RR_MAX-abs(rand())) / (double)RR_MAX-0.5));
|
|
||||||
return 2.0 * CombLCGTaus() - 1.0;
|
return 2.0 * CombLCGTaus() - 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -191,8 +190,8 @@ unsigned int CombLCGTausInt2(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*** gauss ***/
|
/*** gauss ***
|
||||||
|
for speed reasons get two values per pass */
|
||||||
double gauss0(void)
|
double gauss0(void)
|
||||||
{
|
{
|
||||||
static bool gliset = TRUE;
|
static bool gliset = TRUE;
|
||||||
|
|
@ -200,7 +199,8 @@ double gauss0(void)
|
||||||
double fac,r,v1,v2;
|
double fac,r,v1,v2;
|
||||||
if (gliset) {
|
if (gliset) {
|
||||||
do {
|
do {
|
||||||
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); */
|
||||||
|
|
@ -239,18 +239,18 @@ double gauss1(void)
|
||||||
|
|
||||||
void rgauss(double* py1, double* py2)
|
void rgauss(double* py1, double* py2)
|
||||||
{
|
{
|
||||||
double x1, x2, w;
|
double x1, x2, w;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
x1 = 2.0 * CombLCGTaus() - 1.0;
|
x1 = 2.0 * CombLCGTaus() - 1.0;
|
||||||
x2 = 2.0 * CombLCGTaus() - 1.0;
|
x2 = 2.0 * CombLCGTaus() - 1.0;
|
||||||
w = x1 * x1 + x2 * x2;
|
w = x1 * x1 + x2 * x2;
|
||||||
} while ( w >= 1.0 );
|
} while ( w >= 1.0 );
|
||||||
|
|
||||||
w = sqrt( (-2.0 * log( w ) ) / w );
|
w = sqrt( (-2.0 * log( w ) ) / w );
|
||||||
|
|
||||||
*py1 = x1 * w;
|
*py1 = x1 * w;
|
||||||
*py2 = x2 * w;
|
*py2 = x2 * w;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -278,13 +278,11 @@ int poisson(double lambda)
|
||||||
double exprand(double mean)
|
double exprand(double mean)
|
||||||
{
|
{
|
||||||
double expval;
|
double expval;
|
||||||
checkseed();
|
|
||||||
expval = -log(CombLCGTaus()) * mean;
|
expval = -log(CombLCGTaus()) * mean;
|
||||||
return expval;
|
return expval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* seed random number generators immediately
|
/* seed random number generators immediately
|
||||||
* command "setseed"
|
* command "setseed"
|
||||||
* take value of variable rndseed as seed
|
* take value of variable rndseed as seed
|
||||||
|
|
@ -329,4 +327,3 @@ setseedinfo(void)
|
||||||
{
|
{
|
||||||
seedinfo = TRUE;
|
seedinfo = TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue