-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.cpp
287 lines (262 loc) · 4.99 KB
/
random.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "random.h"
long setseed(void) {
time_t lt;
lt=time(NULL);
return (long)lt;
}
/*C routine random number generator from
Numerical Recipes in C: Press et al*/
#define IM1 2147483563
#define IM2 2147483399
#define AM (1.0/IM1)
#define IMM1 (IM1-1)
#define IA1 40014
#define IA2 40692
#define IQ1 53668
#define IQ2 52774
#define IR1 12211
#define IR2 3791
#define NTAB 32
#define NDIV (1+IMM1/NTAB)
#define EPS 1.2e-7
#define RNMX (1.0-EPS)
double ran2(void) {
int j;
long k;
extern long *idum;
static long idum2=123456789;
static long iy=0;
static long iv[NTAB];
double temp;
if (*idum <= 0) {
if (-(*idum) < 1) *idum=1;
else *idum = -(*idum);
idum2=(*idum);
for (j=NTAB+7; j>=0; j--) {
k=(*idum)/IQ1;
*idum=IA1*(*idum-k*IQ1)-k*IR1;
if (*idum < 0) *idum += IM1;
if (j < NTAB) iv[j] = *idum;
}
iy=iv[0];
}
k=(*idum)/IQ1;
*idum=IA1*(*idum-k*IQ1)-k*IR1;
if (*idum < 0) *idum += IM1;
k=idum2/IQ2;
idum2=IA2*(idum2-k*IQ2)-k*IR2;
if (idum2 < 0) idum2 += IM2;
j=iy/NDIV;
iy=iv[j]-idum2;
iv[j]=*idum;
if (iy < 1) iy += IMM1;
if ((temp=AM*iy) > RNMX) return RNMX;
else return temp;
}
double ran2(double min, double max)
{
double r = ran2();
r *= (max - min);
r += min;
return r;
}
int ran2(int min, int max)
{
double r = ran2();
r *= (double)(max - min);
r += min;
return (int)r;
}
int ran2(int max)
{
double r = ran2();
r *= (double)max;
return (int)r;
}
// Returns LnGam(xx)
// From Numerical Recipes in C
double gammln(double xx)
{
double x,y,tmp,ser;
static double cof[6]={76.18009172947146,-86.50532032941677,
24.01409824083091,-1.231739572450155,
0.1208650973866179e-2,-0.5395239384953e-5};
int j;
y=x=xx;
tmp=x+5.5;
tmp -= (x+0.5)*log(tmp);
ser=1.000000000190015;
for (j=0;j<=5;j++) ser += cof[j]/++y;
return -tmp+log(2.5066282746310005*ser/x);
}
// AdamA: Normal Distrubted random number generator
// From Numerical Recipes in C, Press et al. 1988
// Returns a normally distributed random number with zero mean and unit variance
double normrnd(void)
{
extern long *idum;
static int iset=0;
static double gset;
double fac, r, v1, v2;
if (iset == 0)
{
do
{
v1 = 2.0 * ran2() - 1.0;
v2 = 2.0 * ran2() - 1.0;
r = v1 * v1 + v2 * v2;
} while (r >= 1.0);
fac = sqrt(-2.0*log(r) / r);
gset = v1 * fac;
iset = 1;
return v2*fac;
}
else
{
iset = 0;
return gset;
}
}
// Returns random numbers distributed as a gamma distribution
// From Devroye 1986
double gamrnd(double ia, double ib)
{
double b, c, d, u, v, w, x, y, z;
int accept;
double ret = 0;
if (ia == 1.0)
{
// gamma is exponetial (Devroye pg 405)
ret = -ib * log(ran2());
}
else if ((ia < 1) && (ia > 0))
{
c = 1 / ia;
d = 1 / (1 - ia);
accept = 0;
do
{
u = ran2();
v = ran2();
x = pow(u, c);
y = pow(v, d);
z = x + y;
if (z <= 1.0)
accept = 1;
} while (accept != 1);
ret = -ib * log(ran2()) * x / z;
}
else if (ia > 1)
{
b = ia - 1;
c = 3.0 * ia - 0.75;
accept = 0;
do
{
u = ran2();
v = ran2();
w = u * (1 - u);
y = (u - 0.5) * sqrt(c / w);
x = b + y;
if (x >= 0.0)
{
z = 64.0 * pow(w, 3) * pow(v, 2);
if (z <= (1 - 2 * y * y / x))
{ accept = 1; }
else
{
if (log(z) <= (2 * (b * log(x / b) - y)))
{ accept = 1; }
}
}
} while (accept != 1);
ret = ib * x;
}
return ret;
}
// AdamA: Poisson Distrubted random number generator
// For Numerical Recipes in C, Press et al. 1988
// Returns a poisson distributed random number with mean xm
int poissrnd(double xm)
{
//float gammln(float xx);
static double sq, alxm, g, oldm=(-1.0);
double em,t,y;
if (xm < 12.0) {
if (xm != oldm) {
oldm=xm;
g=exp(-xm);
}
em = -1;
t=1.0;
do {
++em;
t *= ran2();
} while (t > g);
} else {
if (xm != oldm) {
oldm=xm;
sq=sqrt(2.0*xm);
alxm=log(xm);
g = xm*alxm - gammln(xm + 1.0);
}
do {
do {
y=tan(PI * ran2());
em=sq*y+xm;
} while (em < 0.0);
em=floor(em);
t=0.9*(1.0+y*y)*exp(em*alxm-gammln(em+1.0)-g);
} while (ran2() > t);
}
return (int)em;
}
double exprnd(double mu)
{
return -mu * log(ran2());
}
int bnlrnd(double pp, int n)
{
int j;
static int nold=(-1);
double am,em,g,angle,p,bnl,sq,t,y;
static double pold=(-1.0),pc,plog,pclog,en,oldg;
p=(pp <= 0.5 ? pp : 1.0-pp);
am=n*p; //This is the mean of the deviate to be produced.
if (n < 25) {
bnl = 0.0;
for (j=1;j<=n;j++)
if (ran2() < p) ++bnl;
} else if (am < 1.0) {
g=exp(-am);
t=1.0;
for (j=0;j<=n;j++) {
t *= ran2();
if (t < g) break;
}
bnl=(j <= n ? j : n);
} else {
if (n != nold) {
en=n;
oldg=gammln(en+1.0);
nold=n;
} if (p != pold) {
pc=1.0-p;
plog=log(p);
pclog=log(pc);
pold=p;
}
sq=sqrt(2.0*am*pc);
do {
do {
angle=PI*ran2();
y=tan(angle);
em=sq*y+am;
} while (em < 0.0 || em >= (en+1.0));
em=floor(em);
t=1.2*sq*(1.0+y*y)*exp(oldg-gammln(em+1.0)-gammln(en-em+1.0)+em*plog+(en-em)*pclog);
} while (ran2() > t); bnl=em;
}
if (p != pp) bnl=n-bnl;
return (int)bnl;
}