TPCCLIB
Toggle main menu visibility
Loading...
Searching...
No Matches
libtpcrand
gaussdev.c
Go to the documentation of this file.
1
4
/*****************************************************************************/
5
#include "tpcclibConfig.h"
6
/*****************************************************************************/
7
#include <stdio.h>
8
#include <stdlib.h>
9
#include <unistd.h>
10
#include <math.h>
11
#include <sys/time.h>
12
#include <time.h>
13
#include <string.h>
14
/*****************************************************************************/
15
#include "
tpcextensions.h
"
16
/*****************************************************************************/
17
#include "
tpcrand.h
"
18
/*****************************************************************************/
19
20
/*****************************************************************************/
27
unsigned
int
drandSeed
(
29
short
int
seed
30
) {
31
unsigned
int
li;
32
#if defined HAVE_TIMESPEC_GET
33
struct
timespec ts;
34
timespec_get(&ts, TIME_UTC);
35
//li=(unsigned int)ts.tv_sec+(unsigned int)ts.tv_nsec+(unsigned int)getpid();
36
li=((ts.tv_sec % 10000)*523 ^ ts.tv_nsec*10) ^ ((getpid() % 1000)*983);
37
#elif defined HAVE_CLOCK_GETTIME
38
struct
timespec ts;
39
clock_gettime(CLOCK_REALTIME, &ts);
40
//li=(unsigned int)ts.tv_sec+(unsigned int)ts.tv_nsec+(unsigned int)getpid();
41
li=((ts.tv_sec % 10000)*523 ^ ts.tv_nsec*10) ^ ((getpid() % 1000)*983);
42
#elif defined HAVE_GETTIMEOFDAY
43
struct
timeval tv;
44
gettimeofday(&tv, 0);
45
li=((tv.tv_sec % 10000)*523 ^ tv.tv_usec*13) ^ ((getpid() % 1000)*983);
46
#else
47
li=(
unsigned
int)time(NULL)+(
unsigned
int)getpid();
48
#endif
49
li+=(
unsigned
int)rand();
50
if
(seed) srand(li);
51
return
(li);
52
}
53
/*****************************************************************************/
54
55
/*****************************************************************************/
66
double
drand
()
67
{
68
double
d, s;
69
s=1.0/(1.0+RAND_MAX);
70
do
{
71
d = ( ( s*rand() + rand() )*s + rand() ) * s;
72
}
while
(d>=1.0);
73
return
d;
74
}
75
/*****************************************************************************/
76
77
/*****************************************************************************/
90
int
drandRange
(
92
unsigned
int
nr,
94
double
*d,
96
double
low,
98
double
up,
100
int
type
101
) {
102
unsigned
int
i;
103
double
dif, v, stl, stu;
104
105
if
(nr<1)
return
0;
106
if
(d==NULL || type<0 || type>1)
return
1;
107
108
dif=up-low;
if
(dif<0.0)
return
2;
109
if
(dif==0.0) {
110
for
(i=0; i<nr; i++) d[i]=low;
111
return
0;
112
}
113
114
if
(type==0) {
115
for
(i=0; i<nr; i++) d[i] =
drand
()*dif + low;
116
}
else
if
(type==1) {
117
stl=copysign(sqrt(fabs(low)),low);
if
(!isnormal(stl)) stl=0.0;
118
stu=copysign(sqrt(fabs(up)), up);
if
(!isnormal(stu)) stu=0.0;
119
dif=stu-stl;
120
for
(i=0; i<nr; i++) {v=
drand
()*dif+stl; d[i]=copysign(v*v, v);}
121
}
122
123
return
0;
124
}
125
/*****************************************************************************/
126
127
/*****************************************************************************/
144
double
drandGaussian
()
145
{
146
static
int
ready=0;
147
static
double
dev;
148
double
fac, rsq, a, b;
149
150
/* If we don't have deviate already, then we'll have to make one */
151
if
(!ready) {
152
do
{
153
a = 2.*
drand
() - 1.0;
154
b = 2.*
drand
() - 1.0;
155
rsq = a*a + b*b;
156
}
while
(rsq>1.0 || rsq==0.0);
157
158
fac = sqrt(-2.0*log(rsq)/rsq);
159
dev=a*fac; ready=1;
160
return
(b*fac);
161
}
else
{
/* dev is ready so return it */
162
ready=0;
163
return
(dev);
164
}
165
}
166
/*****************************************************************************/
167
168
/*****************************************************************************/
178
double
drandExponential
(
180
double
mean
181
) {
182
double
r;
183
do
{
184
r=
drand
();
185
}
while
(r==0.0);
186
return
(-mean*log(r));
187
}
188
/*****************************************************************************/
189
190
/*****************************************************************************/
drand
double drand()
Definition
gaussdev.c:66
drandRange
int drandRange(unsigned int nr, double *d, double low, double up, int type)
Definition
gaussdev.c:90
drandExponential
double drandExponential(double mean)
Get pseudo-random number with exponential distribution.
Definition
gaussdev.c:178
drandGaussian
double drandGaussian()
Get pseudo-random number with normal (Gaussian) distribution with mean 0 and SD 1.
Definition
gaussdev.c:144
drandSeed
unsigned int drandSeed(short int seed)
Make and optionally set the seed for rand(), drand, drandRange, and drandGaussian().
Definition
gaussdev.c:27
tpcextensions.h
Header file for library libtpcextensions.
tpcrand.h
Header file for libtpcrand.
Generated on
for TPCCLIB by
1.17.0