TPCCLIB
Loading...
Searching...
No Matches
gaussdev.c
Go to the documentation of this file.
1
5/*****************************************************************************/
7long int GAUSSDEV_SEED;
8/*****************************************************************************/
9#include "libtpcmodel.h"
10/*****************************************************************************/
11#ifndef RAND_MAX
13#define RAND_MAX 32767
14#endif
16#define RS_SCALE (1.0 / (1.0 + RAND_MAX))
18/*****************************************************************************/
19
20/*****************************************************************************/
30double gaussdev()
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}
55/*****************************************************************************/
56
57/*****************************************************************************/
63unsigned int drandSeed(
65 short int seed
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}
88/*****************************************************************************/
89
90/*****************************************************************************/
93{
94 if(GAUSSDEV_SEED<1L) GAUSSDEV_SEED=893165470L;
95 srand(GAUSSDEV_SEED);
96}
97/*****************************************************************************/
98
99/*****************************************************************************/
112double gaussdev2()
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}
134/*****************************************************************************/
135
136/*****************************************************************************/
142double drand()
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}
151/*****************************************************************************/
152
153/*****************************************************************************/
161 int nr,
163 double *d,
165 double low,
167 double up,
169 int type
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}
194/*****************************************************************************/
195
196/*****************************************************************************/
double drand()
Definition gaussdev.c:142
double gaussdev2()
Definition gaussdev.c:112
#define RAND_MAX
Definition gaussdev.c:13
double gaussdev()
Definition gaussdev.c:30
void init_gaussdev()
Definition gaussdev.c:92
int rand_range(int nr, double *d, double low, double up, int type)
Definition gaussdev.c:159
long int GAUSSDEV_SEED
Definition gaussdev.c:7
unsigned int drandSeed(short int seed)
Make and optionally set the seed for rand(), drand, drandRange, and drandGaussian().
Definition gaussdev.c:63
Header file for libtpcmodel.