1 /* random.h
2 
3    Copyright (c) 1992-2017 Free Software Foundation, Inc.
4 
5    This file is part of GNU MCSim.
6 
7    GNU MCSim is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License
9    as published by the Free Software Foundation; either version 3
10    of the License, or (at your option) any later version.
11 
12    GNU MCSim is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with GNU MCSim; if not, see <http://www.gnu.org/licenses/>
19 
20    Header for random number generator.  See random.c for extensive
21    documentation.
22 
23    Gives prototypes for random functions.
24 */
25 
26 #ifndef RANDOM_H_DEFINED
27 
28 #include <math.h>
29 
30 #include "config.h"
31 
32 /* ----------------------------------------------------------------------------
33    Constants  */
34 
35 #define SEED_MIN     1.0
36 #define SEED_MAX     2147483646.0
37 
38 #ifdef HAVE_LIBGSL
39 #define SEED_DEFAULT 0
40 #else  /* non-gsl version */
41 #define SEED_DEFAULT 314159265.3589793
42 #endif
43 
44 
45 #define EXP1         2.7182818284590452353602875
46 #define INV_SQRT_2PI 0.398942280401432702863
47 #define LOG_SQRT_2PI 0.918938533204672669541
48 #define PI           3.1415926535897932384626433
49 #define SQRT_2       1.414213562373095145475
50 #define SQRT_2PI     2.506628274631000241612
51 #define TWO_SQRTEXP1 3.297442541400256388329
52 
53 
54 /* ----------------------------------------------------------------------------
55    Prototypes  */
56 
57 /* Initialize the random generators, mandatory */
58 void InitRandom (int rank, double dSeed, int bWarmUp);
59 
60 /* Fundamental random generator */
61 double Randoms (void);
62 
63 /* Several types of random variates */
64 double BetaRandom (double alpha, double beta, double a, double b);
65 double BinomialBetaRandom (double Expectation, double alpha, double beta);
66 double BinomialRandom (double p, long n);
67 double CauchyRandom (double dScale);
68 double Chi2Random (double dof);
69 double ExpRandom (double beta);
70 double InvGGammaRandom (double alpha, double beta);
71 double TruncInvGGammaRandom (double alpha, double beta, double a, double b);
72 double GammaRandom (double alpha);
73 double GGammaRandom (double alpha, double beta);
74 double LogNormalRandom (double dGeoMean, double dGeoSD);
75 double GenLogNormalRandom (double dMean, double dSDNorm, double dSDLogNorm);
76 double StudentTRandom (double dof, double dMean, double dSD);
77 double LogUniformRandom (double a, double b);
78 long   NegativeBinomialRandom (double r, double p);
79 double NormalRandom (double dMean, double dSD);
80 double PiecewiseRandom (double min, double a, double b, double max);
81 double PiecewiseVariate (long cDim, double rg_x[], double rg_pdf[],
82                          double rg_Cdf[], int iOrder, double *pVal_pdf);
83 long   PoissonRandom (double mu);
84 double TruncLogNormalRandom (double dGeoMean, double dGeoSD,
85                              double a, double b);
86 double TruncNormalRandom (double dMean, double dSD, double a, double b);
87 double UniformRandom (double a, double b);
88 void   Multinomial (long n, int dim, double *p, double *x);
89 void   WishartRandom (long n, long p, double *t, double *w, double *work);
90 
91 
92 /* ----------------------------------------------------------------------------
93    Utility functions */
94 
95 BOOL   and (BOOL A, BOOL B);
96 double CDFNormal (double z);
97 double DFNormal (double x, double mu, double sd);
98 
99 #ifndef HAVE_ERFC
100 double erfc (double x);
101 #endif
102 
103 double GetSeed (void);
104 double InterpolateX (double rgX[], double rgY[], long lLower, double dY);
105 double lnDFNormal (double x, double mu, double sd);
106 double lnGamma (double x);
107 double lnDFBeta (double x, double alpha, double beta, double min, double max);
108 void   CalcCumulative (long cDim, double *rg_x, double *rg_pdf,
109                        double *rg_Cdf, int  iOrder);
110 void   SetSeed (double dSeed);
111 
112 #define RANDOM_H_DEFINED
113 #endif
114 
115 /* End */
116 
117