1 | // $Id: random_singleton.h 13 2003-06-19 10:00:28Z daniel $ |
---|
2 | |
---|
3 | #ifndef _THEP_RANDOMSINGLETON_ |
---|
4 | #define _THEP_RANDOMSINGLETON_ |
---|
5 | |
---|
6 | |
---|
7 | #include <cstdlib> |
---|
8 | #include <cstdio> |
---|
9 | #include <string> |
---|
10 | |
---|
11 | // GSL INCLUDES |
---|
12 | //////////////////////////// |
---|
13 | #include <gsl/gsl_rng.h> |
---|
14 | #include <gsl/gsl_randist.h> |
---|
15 | |
---|
16 | |
---|
17 | namespace thep_cpp_tools |
---|
18 | { |
---|
19 | /** |
---|
20 | Class defining interface for different random |
---|
21 | stuff. |
---|
22 | */ |
---|
23 | class random_singleton |
---|
24 | { |
---|
25 | private: |
---|
26 | /** |
---|
27 | flag that keeps track of whether an object |
---|
28 | exists or not. |
---|
29 | */ |
---|
30 | static bool instance_; |
---|
31 | |
---|
32 | /** |
---|
33 | pointer to an instance of the class. |
---|
34 | */ |
---|
35 | static random_singleton *singleobj_; |
---|
36 | |
---|
37 | /** |
---|
38 | GSL stuff |
---|
39 | */ |
---|
40 | gsl_rng* r_; |
---|
41 | gsl_ran_discrete_t* gen_; // general purpose distribution |
---|
42 | |
---|
43 | /** |
---|
44 | Private constructor. Only way to |
---|
45 | create object is via get_instance(). |
---|
46 | */ |
---|
47 | random_singleton(); |
---|
48 | |
---|
49 | public: |
---|
50 | /** |
---|
51 | Method used to instantiate an object from this class. |
---|
52 | The seed is zero by default. If user wish to use time |
---|
53 | the user should specify a negative seed. Note that if an |
---|
54 | object already exists the method will return that object |
---|
55 | and hence no new object is created. |
---|
56 | */ |
---|
57 | static random_singleton* get_instance( int seed = 0 ); |
---|
58 | |
---|
59 | |
---|
60 | /** |
---|
61 | A random integer number is returned from a uniform |
---|
62 | distribution between [ \a min,\a max]. The values of |
---|
63 | \a min and \a max are obtained via get_uniform_int_min() |
---|
64 | and get_uniform_int_max() |
---|
65 | */ |
---|
66 | inline long int get_uniform_int() const; |
---|
67 | |
---|
68 | |
---|
69 | /** |
---|
70 | A random number (double precision) is returned |
---|
71 | from a uniform distribution between [ 0, 1 [. |
---|
72 | */ |
---|
73 | inline double get_uniform_double() const; |
---|
74 | |
---|
75 | |
---|
76 | /** |
---|
77 | A random number (double precision) is returned |
---|
78 | from a gaussioan distribution between where \a sigma |
---|
79 | can be specified by caller. \a sigma (\f$\sigma\f$) is |
---|
80 | the standard deviation in a gaussian distribution: \n\n |
---|
81 | \f$f(x) = \frac{1}{\sqrt{2\pi\sigma^2}}e^{-\frac{x^2}{2\sigma^2}}\f$ \n\n |
---|
82 | Default is \a sigma = 1. |
---|
83 | */ |
---|
84 | inline double get_gaussian( const double& sigma ) const; |
---|
85 | |
---|
86 | |
---|
87 | /** |
---|
88 | A random number (double precision) is returned |
---|
89 | from an exponential distribution. \a mu (\f$\mu\f$), the mean, |
---|
90 | can be specified by caller: \f$f(x) = \frac{1}{\mu}e^{-\frac{x}{\mu}}\f$. |
---|
91 | Default is \a mu = 1. |
---|
92 | */ |
---|
93 | inline double get_exponential( const double& mu ) const; |
---|
94 | |
---|
95 | |
---|
96 | /** |
---|
97 | See get_uniform_int()! |
---|
98 | */ |
---|
99 | inline long int get_uniform_min() const; |
---|
100 | |
---|
101 | |
---|
102 | /** |
---|
103 | See get_uniform_int()! |
---|
104 | */ |
---|
105 | inline long int get_uniform_max() const; |
---|
106 | |
---|
107 | |
---|
108 | /** |
---|
109 | Return seed to user for documentation purposes. |
---|
110 | */ |
---|
111 | inline int get_seed() const; |
---|
112 | |
---|
113 | /** |
---|
114 | Set a new seed |
---|
115 | */ |
---|
116 | void set_seed( const int& seed ); |
---|
117 | |
---|
118 | |
---|
119 | /** |
---|
120 | Set probabilities \a p for the general purpose |
---|
121 | distribution. |
---|
122 | */ |
---|
123 | inline void set_general_distribution_prob( const size_t& k, |
---|
124 | const double* p ); |
---|
125 | |
---|
126 | |
---|
127 | /** |
---|
128 | Get a random number (index) from the general purpose |
---|
129 | distribution |
---|
130 | */ |
---|
131 | inline size_t get_rnd_discrete(); |
---|
132 | |
---|
133 | |
---|
134 | /** |
---|
135 | GSL has different types of random genenerators. This function |
---|
136 | returns the one used. |
---|
137 | */ |
---|
138 | std::string get_generator_type() const; |
---|
139 | |
---|
140 | |
---|
141 | /** |
---|
142 | Destructor returning memory using GSL free functions |
---|
143 | */ |
---|
144 | ~random_singleton(); |
---|
145 | }; // random_singleton |
---|
146 | |
---|
147 | #include "random_singleton.icc" |
---|
148 | |
---|
149 | } // namespace thep_c++_tools |
---|
150 | |
---|
151 | #endif |
---|