-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrgauss.c
More file actions
316 lines (255 loc) · 6.44 KB
/
rgauss.c
File metadata and controls
316 lines (255 loc) · 6.44 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
Peter Latham
July, 1997
*/
#include "rgauss.h"
// constructor
rgauss::rgauss(int nn)
{
// set variables and reserve space
this->n = nn;
this->y = new float[n];
/* divide gaussian into n intervals of equal probability.
estimate area by expanding gaussian through quadratic.
width in y of section with probability r is given approximately by
dy = (r/f) - (f'/2f) * (r/f)^2 +[(f'/f)^2/2 - f''/6f)] * (r/f)^3
where dy = y-y0, f=f(y0), f'=df(y0)/dy0, f''=d^f(y0)/dy0^2,
f is gaussian probability distribution. */
int n1, n2;
// sqrt2pi = sqrt(2*pi)
float r, sqrt2pi = 2.5066283, y0;
r=1/((float) n);
// determine where to start. that depends on whether n is odd or even.
if (n%2 == 0)
{
y0 = 0;
n1=n/2;
n2=n/2-1;
}
else
{
float dum = 0.5*r*sqrt2pi;
y0 = dum*(1+dum*dum/6);
y[n/2] = 0;
n1=n/2+1;
n2=n/2-1;
}
for (int i=0; i < n/2; i++)
{
float dum = r*sqrt2pi*exp(y0*y0/2);
float dy = dum*(1+dum*(0.5*y0+dum*(0.5+y0*y0)/3));
y[n1+i] = y0+0.5*dy;
y[n2-i] = -(y0+0.5*dy);
y0 += dy;
}
}
// destructor
rgauss::~rgauss()
{
delete [] y;
}
// overloaded function.
float rgauss::ran(float sigma)
{
/*
return random number with gaussian distribution.
INPUT:
sigma - standard deviation of Gaussian.
RETURNS:
gaussian distributed random number with standard deviation sigma.
*/
return sigma*y[(int) (n*drand())];
}
float rgauss::ran(float sigma, float xmin)
{
/*
return random number with gaussian distribution truncated below.
INPUT:
sigma - standard deviation of Gaussian.
xmin - minimum cutoff of truncated gaussian.
RETURNS:
truncated (from below) gaussian distributed random number
with standard deviation (before truncation) sigma.
*/
int i;
// find cutoff
for (i=0; i < n; i++) if (sigma*y[i] >= xmin) break;
if (i >= n) i=n-1;
float alpha = ((float) i) / ((float) n);
float x = sigma*y[(int) (n*(alpha + (1-alpha)*drand()))];
return x;
}
float rgauss::ran(float sigma, float xmin, float xmax)
{
/*
return random number with gaussian distribution truncated below
and above.
INPUT:
sigma - standard deviation of Gaussian.
xmin - minimum cutoff of truncated gaussian.
xmax - maximum cutoff of truncated gaussian.
RETURNS:
truncated (from below and above) gaussian distributed random
number with standard deviation (before truncation) sigma.
*/
// check
if (xmax < xmin)
{
float tmp = xmin;
xmin = xmax;
xmax = tmp;
}
int i, j;
// find minimum cutoff
for (i=0; i < n; i++) if (sigma*y[i] >= xmin) break;
if (i >= n) i=n-1;
// find maximum cutoff
for (j=n-1; j >= 0; j--) if(sigma*y[j] <= xmax) break;
if (j < 0) j=0;
// check
if (i == j)
{
if (i == 0) j=1;
else i = j-1;
}
float alpha = ((float) i) / ((float) n);
float beta = ((float) j) / ((float) n);
float x = sigma*y[(int) (n*(alpha + (beta-alpha)*drand()))];
return x;
}
float* rgauss::ran(float sigma, int ntot)
{
/*
return array of random numbers with gaussian distribution.
INPUT:
sigma - standard deviation of Gaussian.
ntot - total number of random numbers to return.
RETURNS:
array of gaussian distributed random numbers with standard
deviation sigma.
*/
float* x = new float[ntot];
for (int i=0; i < ntot; i++) x[i] = sigma*y[(int) (n*drand())];
return x;
}
float* rgauss::ran(float sigma, int ntot, float xmin)
{
/*
return array of random numbers with gaussian distribution truncated
below.
INPUT:
sigma - standard deviation of Gaussian.
ntot - total number of random numbers to return.
xmin - minimum cutoff of truncated gaussian.
RETURNS:
array of truncated (from below) gaussian distributed random numbers
with standard deviation (before truncation) sigma.
*/
int i;
// find cutoff
for (i=0; i < n; i++) if (sigma*y[i] >= xmin) break;
if (i >= n) i=n-1;
float alpha = ((float) i) / ((float) n);
float* x = new float[ntot];
for (i=0; i < ntot; i++)
x[i] = sigma*y[(int) (n*(alpha + (1-alpha)*drand()))];
return x;
}
float* rgauss::ran(float sigma, int ntot, float xmin, float xmax)
{
/*
return array of random numbers with gaussian distribution truncated
below and above.
INPUT:
sigma - standard deviation of Gaussian.
ntot - total number of random numbers to return.
xmin - minimum cutoff of truncated gaussian.
xmax - maximum cutoff of truncated gaussian.
RETURNS:
array of truncated (from below and above) gaussian distributed random
numbers with standard deviation (before truncation) sigma.
*/
// check
if (xmax < xmin)
{
float tmp = xmin;
xmin = xmax;
xmax = tmp;
}
int i, j;
// find minimum cutoff
for (i=0; i < n; i++) if (sigma*y[i] >= xmin) break;
if (i >= n) i=n-1;
// find maximum cutoff
for (j=n-1; j >= 0; j--) if(sigma*y[j] <= xmax) break;
if (j < 0) j=0;
// check
if (i == j)
{
if (i == 0) j=1;
else i = j-1;
}
float alpha = ((float) i) / ((float) n);
float beta = ((float) j) / ((float) n);
float* x = new float[ntot];
for (i=0; i < ntot; i++)
x[i] = sigma*y[(int) (n*(alpha + (beta-alpha)*drand()))];
return x;
}
float* rgauss::ran(
float sigma, int ntot, float xmin, float xmax,
float& alpha, float& beta)
{
/*
return array of random numbers with gaussian distribution truncated
below and above.
INPUT:
sigma - standard deviation of Gaussian.
ntot - total number of random numbers to return.
xmin - minimum cutoff of truncated gaussian.
xmax - maximum cutoff of truncated gaussian.
alpha, beta-
- if both these variables are nonnegative, then they are
used as cutoff values instead of xmin and xmax.
they are used for repeated calls to gauss with
the same xmin and xmax, since it can take a long
time to compute alpha and beta.
OUTPUT:
alpha, beta
- returned for use on next call, if desired. see above.
RETURNS:
array of truncated (from below and above) gaussian distributed random
numbers with standard deviation (before truncation) sigma.
*/
float* x = new float[ntot];
if (alpha < 0 || beta < 0)
{
// ---compute alpha and beta from xmin and xmax
// check
if (xmax < xmin)
{
float tmp = xmin;
xmin = xmax;
xmax = tmp;
}
int i, j;
// find minimum cutoff
for (i=0; i < n; i++) if (sigma*y[i] >= xmin) break;
if (i >= n) i=n-1;
// find maximum cutoff
for (j=n-1; j >= 0; j--) if(sigma*y[j] <= xmax) break;
if (j < 0) j=0;
// check
if (i == j)
{
if (i == 0) j=1;
else i = j-1;
}
alpha = ((float) i) / ((float) n);
beta = ((float) j) / ((float) n);
}
// generate random numbers
for (int i=0; i < ntot; i++)
x[i] = sigma*y[(int) (n*(alpha + (beta-alpha)*drand()))];
return x;
}