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
//printf("seed := %u\n", li); printf("RAND_MAX := %u\n", RAND_MAX);
52
return
(li);
53
}
54
/*****************************************************************************/
55
56
/*****************************************************************************/
67
double
drand
()
68
{
69
double
d, s;
70
s=1.0/(1.0+RAND_MAX);
71
do
{
72
d = ( ( s*rand() + rand() )*s + rand() ) * s;
73
}
while
(d>=1.0);
74
return
d;
75
}
76
/*****************************************************************************/
77
78
/*****************************************************************************/
91
int
drandRange
(
93
unsigned
int
nr,
95
double
*d,
97
double
low,
99
double
up,
101
int
type
102
) {
103
unsigned
int
i;
104
double
dif, v, stl, stu;
105
106
if
(nr<1)
return
0;
107
if
(d==NULL || type<0 || type>1)
return
1;
108
109
dif=up-low;
if
(dif<0.0)
return
2;
110
if
(dif==0.0) {
111
for
(i=0; i<nr; i++) d[i]=low;
112
return
0;
113
}
114
115
if
(type==0) {
116
for
(i=0; i<nr; i++) d[i] =
drand
()*dif + low;
117
}
else
if
(type==1) {
118
stl=copysign(sqrt(fabs(low)),low);
if
(!isnormal(stl)) stl=0.0;
119
stu=copysign(sqrt(fabs(up)), up);
if
(!isnormal(stu)) stu=0.0;
120
dif=stu-stl;
121
for
(i=0; i<nr; i++) {v=
drand
()*dif+stl; d[i]=copysign(v*v, v);}
122
}
123
124
return
0;
125
}
126
/*****************************************************************************/
127
128
/*****************************************************************************/
145
double
drandGaussian
()
146
{
147
static
int
ready=0;
148
static
double
dev;
149
double
fac, rsq, a, b;
150
151
/* If we don't have deviate already, then we'll have to make one */
152
if
(!ready) {
153
do
{
154
a = 2.*
drand
() - 1.0;
155
b = 2.*
drand
() - 1.0;
156
rsq = a*a + b*b;
157
}
while
(rsq>1.0 || rsq==0.0);
158
159
fac = sqrt(-2.0*log(rsq)/rsq);
160
dev=a*fac; ready=1;
161
return
(b*fac);
162
}
else
{
/* dev is ready so return it */
163
ready=0;
164
return
(dev);
165
}
166
}
167
/*****************************************************************************/
168
169
/*****************************************************************************/
179
double
drandExponential
(
181
double
mean
182
) {
183
double
r;
184
do
{
185
r=
drand
();
186
}
while
(r==0.0);
187
return
(-mean*log(r));
188
}
189
/*****************************************************************************/
190
191
/*****************************************************************************/
drand
double drand()
Definition
gaussdev.c:67
drandRange
int drandRange(unsigned int nr, double *d, double low, double up, int type)
Definition
gaussdev.c:91
drandExponential
double drandExponential(double mean)
Get pseudo-random number with exponential distribution.
Definition
gaussdev.c:179
drandGaussian
double drandGaussian()
Get pseudo-random number with normal (Gaussian) distribution with mean 0 and SD 1.
Definition
gaussdev.c:145
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.18.0