-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrando2.h
271 lines (235 loc) · 5.36 KB
/
rando2.h
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
/* -------------------------------- rando.h ------------------------------ */
/* Routine per la generazione dei pattern casuali (presuppone include math.h)
(dovrebbero essere portabili) */
/* float drand49(): ritorna un numero float tra 0.0 e 1.0
float srand49(long): inizializza il seme e ritorna un numero random
compreso tra 0.0 e 1.0
(tratto da numerical recipes) */
static long rand49_idum=-77531;
static long rand49_idum2=-77531;
static long rand50_idum=-77531;
#define M 714025
#define IA 1366
#define IC 150889
float drand49()
{
static long iy,ir[98];
static int iff=0;
int j;
if (rand49_idum < 0 || iff == 0) {
iff=1;
if((rand49_idum=(IC-rand49_idum) % M)<0)
rand49_idum=(-rand49_idum);
for (j=1;j<=97;j++) {
rand49_idum=(IA*(rand49_idum)+IC) % M;
ir[j]=(rand49_idum);
}
rand49_idum=(IA*(rand49_idum)+IC) % M;
iy=(rand49_idum);
}
j=1 + 97.0*iy/M;
if (j > 97 || j < 1) printf("RAN2: This cannot happen.");
iy=ir[j];
rand49_idum=(IA*(rand49_idum)+IC) % M;
ir[j]=(rand49_idum);
return (float) iy/M;
}
float drand49b()
{
static long iy,ir[98];
static int iff=0;
int j;
if (rand49_idum2 < 0 || iff == 0) {
iff=1;
if((rand49_idum2=(IC-rand49_idum2) % M)<0)
rand49_idum2=(-rand49_idum2);
for (j=1;j<=97;j++) {
rand49_idum2=(IA*(rand49_idum2)+IC) % M;
ir[j]=(rand49_idum2);
}
rand49_idum2=(IA*(rand49_idum2)+IC) % M;
iy=(rand49_idum2);
}
j=1 + 97.0*iy/M;
if (j > 97 || j < 1) printf("RAN2: This cannot happen.");
iy=ir[j];
rand49_idum2=(IA*(rand49_idum2)+IC) % M;
ir[j]=(rand49_idum2);
return (float) iy/M;
}
float srand49(seme)
long seme;
{
rand49_idum=(-seme);
return drand49();
}
float srand49b(seme2)
long seme2;
{
rand49_idum2=(-seme2);
return drand49b();
}
float drand50()
{
static long iy,ir[98];
static int iff=0;
int j;
if (rand50_idum < 0 || iff == 0) {
iff=1;
if((rand50_idum=(IC-rand50_idum) % M)<0)
rand50_idum=(-rand50_idum);
for (j=1;j<=97;j++) {
rand50_idum=(IA*(rand50_idum)+IC) % M;
ir[j]=(rand50_idum);
}
rand50_idum=(IA*(rand50_idum)+IC) % M;
iy=(rand50_idum);
}
j=1 + 97.0*iy/M;
if (j > 97 || j < 1) printf("RAN2: This cannot happen.");
iy=ir[j];
rand50_idum=(IA*(rand50_idum)+IC) % M;
ir[j]=(rand50_idum);
return (float) iy/M;
}
float srand50(seme)
long seme;
{
rand50_idum=(-seme);
return drand50();
}
#undef M
#undef IA
#undef IC
/* Generatore random di bit 0,1 con probabilita` 1/2 , 1/2 */
#define IB1 1
#define IB2 2
#define IB5 16
#define IB18 131072
#define MASK IB1+IB2+IB5
static unsigned long iseed=31277;
int srand10(seme)
long seme;
{
iseed=seme;
}
int drand10()
{
if (iseed & IB18) {
iseed=((iseed ^ MASK) << 1) | IB1;
return 1;
} else {
iseed <<= 1;
return 0;
}
}
#undef MASK
#undef IB18
#undef IB5
#undef IB2
#undef IB1
/* Ritorna un numero casuale con distribuzione normale (media nulla
varianza unitaria */
float gauss()
{
static int iset=0;
static float gset;
float fac,r,v1,v2;
if (iset == 0) {
do {
v1=2.0*drand49()-1.0;
v2=2.0*drand49()-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;
}
}
float gauss2()
{
static int iset=0;
static float gset;
float fac,r,v1,v2;
if (iset == 0) {
do {
v1=2.0*drand49b()-1.0;
v2=2.0*drand49b()-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;
}
}
float gammln(float 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);
}
#define PI 3.141592654
float binom(float pp,int n)
{
int j;
static int nold=(-1);
float am,em,g,angle,p,bnl,sq,t,y;
static float pold=(-1.0),pc,plog,pclog,en,oldg;
p=(pp<=0.5 ? pp:1.0-pp);
am=n*p;
if(n<25) {
bnl=0.0;
for(j=1;j<=n;j++)
if(drand49()<p) ++bnl;
} else if (am<1.0) {
g=exp(-am);
t=1.0;
for(j=0;j<=n;j++) {
t*=drand49();
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*drand49();
y=tan(angle);
em=sq*y+am;
} while(em<0.0 || em>=(en+1.0));
em=floor(em);
t=1.2*sqrt(1.0+y*y)*exp(oldg-gammln(em+1.0)
-gammln(en-em+1.0)+em*plog+(en-em)*pclog);
} while(drand49()>t);
bnl=em;
}
if(p!=pp) bnl=n-bnl;
return bnl;
}
#undef PI