1
/*===========================================================================
2
Copyright (C) 1995-2009 European Southern Observatory (ESO)
4
This program is free software; you can redistribute it and/or
5
modify it under the terms of the GNU General Public License as
6
published by the Free Software Foundation; either version 2 of
7
the License, or (at your option) any later version.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public
15
License along with this program; if not, write to the Free
16
Software Foundation, Inc., 675 Massachusetss Ave, Cambridge,
19
Corresponding concerning ESO-MIDAS should be addressed as follows:
20
Internet e-mail: midas@eso.org
21
Postal address: European Southern Observatory
22
Data Management Division
23
Karl-Schwarzschild-Strasse 2
24
D 85748 Garching bei Muenchen
30
===========================================================================*/
38
#define PI 3.141592654 /* as in stroustrup */
41
/* -------------------------------------------------------------------
44
purpose: draw random numbers between 0 and 1 under an uniform law
45
-------------------------------------------------------------------*/
59
float random_local(idum)
63
static long ix1,ix2,ix3;
70
if (*idum<0 || iff==0)
73
ix1=(IC1-(*idum)) % M1;
74
ix1=(IA1*ix1+IC1) % M1;
76
ix1=(IA1*ix1+IC1) % M1;
80
ix1=(IA1*ix1+IC1) % M1;
81
ix2=(IA2*ix2+IC2) % M2;
82
r[j]=(ix1+ix2*RM2)*RM1;
86
ix1=(IA1*ix1+IC1) % M1;
87
ix2=(IA2*ix2+IC2) % M2;
88
ix3=(IA3*ix3+IC3) % M3;
90
if(j>97 || j<1) {printf("je me suis plantee\n"); j=abs(*idum);};
92
r[j]=(ix1+ix2*RM2)*RM1;
96
/* -------------------------------------------------------------------
99
purpose: draw numbers under an poisson law
100
-------------------------------------------------------------------*/
102
float poisson(xm,idum)
108
static float sq,alxm,g,oldm=(-1.0);
110
float random_local(),lngam();
124
t*=random_local(idum);
134
g=xm*alxm-lngam(xm+1.0);
140
y=tan(PI*random_local(idum));
144
t=.9*(1.0+y*y)*exp(em*alxm-lngam(em+1.0)-g);
145
}while(random_local(idum)>t);
150
/******************************************************/
157
double cof[6],stp,half=.5,one=1.,fpf=5.5,x,tmp,ser;
167
cof[1]=(-86.50532033);
169
cof[3]=(-1.231739516);
170
cof[4]=0.120858003e-2;
171
cof[5]=(-0.536382e-5);
184
tmp=(x+half)*log(tmp)-tmp;
191
gama1=tmp+log(stp*ser);
196
gama=log(pi/sin(pi))-gama;
203
/* -------------------------------------------------------------------
206
purpose: draw numbers under an gaussian law
207
-------------------------------------------------------------------*/
209
float gauss(sig,idum)
216
float random_local();
218
while( (z = pow(x=random_local(idum)-0.5,2.0) + pow(random_local(idum)-0.5,2.0) ) > 0.25 );
219
while ((r=random_local(idum))<=0.0);
221
return sig*sqrt(-2.0*log(r)/z)*x;