-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGSLRandomBase.h
74 lines (57 loc) · 2.34 KB
/
GSLRandomBase.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
#ifndef GSL_RANDOM_BASE_H
#define GSL_RANDOM_BASE_H
//
// GNU Scientific Library includes
//
#include "gsl/gsl_rng.h"
/**
A base class for GNU Scientific Library (GSL) random number
functions. The setup, initialization and clean-up is the same for
all GSL random number functions. This class abstracts away these
details, placing the stup and initialization in the class
constructor and the clean-up in the class destructor. The class
constructor is passed a seed value for the random number generator.
A class that provides access to one or more GSL random number
functions should be derived from this class. This class must
provide an implementation for the nextRandVal() pure virtual
function. The nextRandVal will call the specific random
number function (for example gsl_ran_ugaussian() for Gaussian
distribution or gsl_ran_flat() for a flat random number
distribution).
This class uses the default random number generator. At least on
Windows XP using the Visual C++ 6.0 compiler the type definitions
for the random functions (for example gsl_rng_mt19937 or
gsl_rng_knuthran) would not link properly. Perhaps they are not
properly exported from the pre-built library.
I decided to use the GSL because is is supported on all major
platforms (UNIX, Linux and Windows) and provides high quality
pseudo-random number generation support. The standard POSIX rand()
function is notorious for its poor quality. While the random()
function on UNIX provides better pseudo-random number quality, but
is still not as good as functions like MT19937.
*/
class GSLRandomBase {
private:
GSLRandomBase(const GSLRandomBase &rhs);
protected:
gsl_rng *state() {
return rStatePtr_;
}
gsl_rng *rStatePtr_;
public:
GSLRandomBase(int seedVal) {
const gsl_rng_type *T;
// The gsl_rng_env_setup() function returns a pointer to the
// type gsl_rng_mt19937, which is associated with the MT19937
// random number generator.
T = gsl_rng_env_setup();
// Allocate a random number state structure
rStatePtr_ = gsl_rng_alloc(T);
// set the seed
gsl_rng_set(rStatePtr_, seedVal);
} // GSLRandomBase constructor
virtual ~GSLRandomBase() {
} // GSLRandomBase destructor
virtual double nextRandVal() = 0;
};
#endif