[go: up one dir, main page]

Menu

[7446bb]: / src / rng.h  Maximize  Restore  History

Download this file

90 lines (78 with data), 2.3 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
/*
* See COPYING file distributed along with the psignifit package for
* the copyright and license terms
*/
#ifndef RNG_H
#define RNG_H
#include <cstdlib>
#include <cmath>
#include "errors.h"
class PsiRandom
{
public:
PsiRandom ( void ) {}
double rngcall ( void );
virtual double draw ( void ) { throw NotImplementedError(); }
virtual PsiRandom * clone ( void ) const {throw NotImplementedError(); }
};
class GaussRandom : public PsiRandom
{
private:
double mu;
double sigma;
bool good;
double x1;
double x2;
double w;
double y;
public:
GaussRandom ( double mean=0, double standarddeviation=1 ) : mu ( mean ), sigma ( standarddeviation ), good ( false ) {}
double draw ( void ); ///< draw a random number using box muller transform
PsiRandom * clone ( void ) const { return new GaussRandom(*this); }
};
class UniformRandom : public PsiRandom
{
private:
double lower;
double upper;
public:
UniformRandom ( double low=0, double up=1 ) : lower(low), upper(up) {}
double draw ( void ) { return (upper-lower)*rngcall()+lower; }
PsiRandom * clone ( void ) const { return new UniformRandom(*this); }
};
class BinomialRandom : public PsiRandom
{
private:
int n;
double p;
public:
BinomialRandom ( int number, double probability ) : n(number), p(probability) {}
double draw ( void );
void setprm ( int number, double probability ) { n = number; p = probability; }
PsiRandom * clone ( void ) const { return new BinomialRandom(*this); }
};
class GammaRandom : public PsiRandom
{
private:
double k;
double theta;
GaussRandom grng;
public:
GammaRandom ( double shape, double scale ) : k (shape), theta(scale), grng() {}
double draw ( void ); ///< draw a random number
PsiRandom * clone ( void ) const { return new GammaRandom(*this); }
};
class BetaRandom : public PsiRandom
{
private:
double alpha;
double beta;
GammaRandom grnga;
GammaRandom grngb;
public:
BetaRandom ( double alpha, double beta ) : alpha(alpha), beta(beta), grnga (alpha, 1), grngb (beta, 1) {}
double draw ( void ); ///< draw a random number
PsiRandom * clone ( void ) const { return new BetaRandom(*this); }
};
void setSeed(long int seedval);
#endif