TPCCLIB
Loading...
Searching...
No Matches
gaussdev.c File Reference

normally distributed (Gaussian) deviate with zero mean and unit variance. More...

#include "libtpcmodel.h"

Go to the source code of this file.

Macros

#define RAND_MAX   32767
 

Functions

double gaussdev ()
 
unsigned int drandSeed (short int seed)
 Make and optionally set the seed for rand(), drand, drandRange, and drandGaussian().
 
void init_gaussdev ()
 
double gaussdev2 ()
 
double drand ()
 
int rand_range (int nr, double *d, double low, double up, int type)
 

Variables

long int GAUSSDEV_SEED
 

Detailed Description

normally distributed (Gaussian) deviate with zero mean and unit variance.

Author
Vesa Oikonen

Definition in file gaussdev.c.

Macro Definition Documentation

◆ RAND_MAX

#define RAND_MAX   32767

Max number for rand()

Definition at line 13 of file gaussdev.c.

Referenced by bootstrap(), drand(), and random_shuffle().

Function Documentation

◆ drand()

double drand ( )

Alternative function to rand() which returns a double precision floating point number in the range of [0,1].

Returns
Random value in the range [0,1].
See also
rand_range, gaussdev

Definition at line 142 of file gaussdev.c.

143{
144 double d, s;
145 s=1.0/(1.0+RAND_MAX);
146 do {
147 d = ( ( s*rand() + rand() )*s + rand() ) * s;
148 } while(d>=1.0);
149 return d;
150}
#define RAND_MAX
Definition gaussdev.c:13

Referenced by gaussdev(), gaussdev2(), rand_range(), tgoRandomParameters(), and tgoRandomParametersST().

◆ drandSeed()

unsigned int drandSeed ( short int seed)

Make and optionally set the seed for rand(), drand, drandRange, and drandGaussian().

Uses microseconds from the computer clock and process ID to reduce the chance of getting the same seed for simultaneously executing program threads and instances.

Returns
Returns the seed for srand().
Parameters
seedAlso sets seed with srand (1) or not (0)

Definition at line 63 of file gaussdev.c.

66 {
67 unsigned int li;
68#ifdef HAVE_TIMESPEC_GET
69 struct timespec ts;
70 timespec_get(&ts, TIME_UTC);
71 li=((ts.tv_sec % 10000)*523 ^ ts.tv_nsec*10) ^ ((getpid() % 1000)*983);
72#elif defined HAVE_CLOCK_GETTIME
73 struct timespec ts;
74 clock_gettime(CLOCK_REALTIME, &ts);
75 li=((ts.tv_sec % 10000)*523 ^ ts.tv_nsec*10) ^ ((getpid() % 1000)*983);
76#elif defined HAVE_GETTIMEOFDAY
77 struct timeval tv;
78 gettimeofday(&tv, 0);
79 li=((tv.tv_sec % 10000)*523 ^ tv.tv_usec*13) ^ ((getpid() % 1000)*983);
80#else
81 li=(unsigned int)time(NULL)+(unsigned int)getpid();
82#endif
83 li+=(unsigned int)rand();
84 if(seed) srand(li);
85 //printf("seed := %u\n", li); printf("RAND_MAX := %u\n", RAND_MAX);
86 return(li);
87}

Referenced by tgo().

◆ gaussdev()

double gaussdev ( )

Applies the polar form of Box-Müller transform to produce pseudo-random numbers with Gaussian (normal) distribution which has a zero mean and standard deviation of one. Box GEP, Muller ME. A note on the generation of random normal deviates. Annals of Mathematical Statistics, Volume 29, Issue 2, 1958, 610-611. Available from JSTOR https://www.jstor.org/

Returns
Returns the pseudo-random number.
See also
gaussdev2, drand

Definition at line 30 of file gaussdev.c.

31{
32 static int ready=0, first=1;
33 static double dev;
34 double fac, rsq, a, b;
35 if(first) {first=0; init_gaussdev();}
36
37 /* If we don't have deviate already, then we'll have to make one */
38 if(!ready) {
39 do {
40 //a = 2.*(double)rand()/(double)RAND_MAX - 1.0;
41 //b = 2.*(double)rand()/(double)RAND_MAX - 1.0;
42 a = 2.*drand() - 1.0;
43 b = 2.*drand() - 1.0;
44 rsq = a*a + b*b;
45 } while (rsq>=1.0 || rsq==0.0);
46
47 fac = sqrt(-2.0*log(rsq)/rsq);
48 dev=a*fac; ready=1;
49 return(b*fac);
50 } else { /* dev is ready so return it */
51 ready=0;
52 return(dev);
53 }
54}
double drand()
Definition gaussdev.c:142
void init_gaussdev()
Definition gaussdev.c:92

◆ gaussdev2()

double gaussdev2 ( )

Applies the polar form of Box-Müller transform to produce pseudo-random numbers with Gaussian (normal) distribution which has a zero mean and standard deviation of one. This function does never set seed, like gaussdev() does, therefore set seed for random number generator before first calling this routine, for example with srand(time(NULL));

Box GEP, Muller ME. A note on the generation of random normal deviates. Annals of Mathematical Statistics, Volume 29, Issue 2, 1958, 610-611. Available from JSTOR https://www.jstor.org/

Returns
Returns the pseudo-random number.
See also
gaussdev, drand

Definition at line 112 of file gaussdev.c.

113{
114 static int ready=0;
115 static double dev;
116 double fac, rsq, a, b;
117
118 /* If we don't have deviate already, then we'll have to make one */
119 if(!ready) {
120 do {
121 a = 2.*drand() - 1.0;
122 b = 2.*drand() - 1.0;
123 rsq = a*a + b*b;
124 } while (rsq>=1.0 || rsq==0.0);
125
126 fac = sqrt(-2.0*log(rsq)/rsq);
127 dev=a*fac; ready=1;
128 return(b*fac);
129 } else { /* dev is ready so return it */
130 ready=0;
131 return(dev);
132 }
133}

◆ init_gaussdev()

void init_gaussdev ( )

Initiate random number generator for gaussdev()

Definition at line 92 of file gaussdev.c.

93{
94 if(GAUSSDEV_SEED<1L) GAUSSDEV_SEED=893165470L;
95 srand(GAUSSDEV_SEED);
96}
long int GAUSSDEV_SEED
Definition gaussdev.c:7

Referenced by gaussdev().

◆ rand_range()

int rand_range ( int nr,
double * d,
double low,
double up,
int type )

Fills the double array with random numbers between specified limits. Set seed for random number generator before calling this routine, for example with srand(time(NULL));

Returns
Returns 0 when successful, otherwise <> 0.
Parameters
nrNr of values in double array
dPointer to allocated double array
lowLower limit for random values
upUpper limit for random values
typeDistribution: 0=even, 1=square-root transformation

Definition at line 159 of file gaussdev.c.

170 {
171 int i;
172 double dif, v, stl, stu;
173
174 if(nr<1) return 0;
175 if(d==NULL || type<0 || type>1) return 1;
176
177 dif=up-low; if(dif<0.0) return 2;
178 if(dif==0.0) {
179 for(i=0; i<nr; i++) d[i]=low;
180 return 0;
181 }
182
183 if(type==0) {
184 for(i=0; i<nr; i++) d[i] = drand()*dif + low;
185 } else if(type==1) {
186 stl=copysign(sqrt(fabs(low)),low); if(!isnormal(stl)) stl=0.0;
187 stu=copysign(sqrt(fabs(up)), up); if(!isnormal(stu)) stu=0.0;
188 dif=stu-stl;
189 for(i=0; i<nr; i++) {v=drand()*dif+stl; d[i]=copysign(v*v, v);}
190 }
191
192 return 0;
193}

Variable Documentation

◆ GAUSSDEV_SEED

long int GAUSSDEV_SEED

Seed for random number generator

Definition at line 7 of file gaussdev.c.

Referenced by init_gaussdev().