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 Massachusetts Ave, Cambridge,
19
Correspondence 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
26
===========================================================================*/
28
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
.AUTHOR P.Grosbol, IPG/ESO
33
.KEYWORDS Random numbers, exponential, gaussian
34
.COMMENT Algorithm is adapted from 'Numerical Recipes in C' p.214
35
.VERSION 1.0 1990-Nov-07 : Creation, PJG
36
.VERSION 1.1 1994-May-28 : Change name of constant PI, PJG
37
.VERSION 1.2 1995-Mar-09 : Correct error + double gammln, PJG
40
-----------------------------------------------------------------------*/
41
#include <math.h> /* Standard mathematical library */
43
#include <proto_gen.h>
45
#define PICONST 3.141592654
48
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
49
.PURPOSE compute random number taken from an Exponential disribution
51
.RETURN exponential distributed random number
52
-----------------------------------------------------------------------*/
53
int *pseed; /* pointer to seed if <0 initiate seq. */
65
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
66
.PURPOSE compute random number taken from a Gaussian disribution
67
with zero mean and unit standard deviation.
68
.RETURN gaussian distributed random number
69
-----------------------------------------------------------------------*/
70
int *pseed; /* pointer to seed if <0 initiate seq. */
72
double r, fac, v1, v2;
74
if (gflag) { gflag = 0; return gval; }
77
v1 = 2.0*randm(pseed) - 1.0;
78
v2 = 2.0*randm(pseed) - 1.0;
81
fac = sqrt( -2.0*log(r)/r );
88
double gamdev(ia,pseed)
89
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
90
.PURPOSE compute random number taken from a Gamma disribution of
91
interger order of 'ia'
92
.RETURN gamma distributed random number
93
-----------------------------------------------------------------------*/
94
int ia; /* integer order of gamma function */
95
int *pseed; /* pointer to seed if <0 initiate seq. */
98
double am, e, s, v1, v2, x, y;
100
if (ia < 1) return 0.0;
103
for (j=0; j<ia; j++) x *= randm(pseed);
110
v1 = 2.0*randm(pseed) - 1.0;
111
v2 = 2.0*randm(pseed) - 1.0;
112
} while (v1*v1+v2*v2 > 1.0);
115
s = sqrt(2.0*am+1.0);
118
e = (1.0+y*y)*exp(am*log(x/am)-s*y);
119
} while (randm(pseed) > e);
127
static double sq, alxm, g, oldm=(-1.0);
129
double poidev(xm,pseed)
130
/.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
131
.PURPOSE compute random number taken from a Poisson disribution
133
.RETURN poisson distributed random number
134
-----------------------------------------------------------------------./
135
double xm; /. mean value of poisson distribution ./
136
int *pseed; /. pointer to seed if <0 initiate seq. ./
138
double em, t, y, gammln();
156
g = xm*alxm - gammln(xm+1.0);
160
y = tan(PICONST*randm(pseed));
164
t = 0.9*(1.0+y*y)*exp(em*alxm-gammln(em+1.0)-g);
165
} while (randm(pseed) > t);