1 | // $Id: random_singleton.h 303 2005-04-30 16:17:35Z peter $ |
---|
2 | |
---|
3 | #ifndef _theplu_utility_random_singleton_ |
---|
4 | #define _theplu_utility_random_singleton_ |
---|
5 | |
---|
6 | |
---|
7 | // #include <cstdlib> |
---|
8 | // #include <cstdio> |
---|
9 | #include <string> |
---|
10 | |
---|
11 | #include <gsl/gsl_rng.h> |
---|
12 | #include <gsl/gsl_randist.h> |
---|
13 | |
---|
14 | namespace theplu { |
---|
15 | namespace utility { |
---|
16 | |
---|
17 | /** |
---|
18 | Class defining interface for different random |
---|
19 | stuff. |
---|
20 | */ |
---|
21 | class random_singleton |
---|
22 | { |
---|
23 | private: |
---|
24 | /** |
---|
25 | pointer to an instance of the class. |
---|
26 | */ |
---|
27 | static random_singleton *singleobj_; |
---|
28 | |
---|
29 | /** |
---|
30 | GSL stuff |
---|
31 | */ |
---|
32 | gsl_rng* r_; |
---|
33 | gsl_ran_discrete_t* gen_; // general purpose distribution |
---|
34 | |
---|
35 | /** |
---|
36 | Private constructor. Only way to |
---|
37 | create object is via get_instance(). |
---|
38 | */ |
---|
39 | random_singleton(); |
---|
40 | |
---|
41 | public: |
---|
42 | /** |
---|
43 | Method used to instantiate an object from this class. |
---|
44 | The seed is zero by default. If user wish to use time |
---|
45 | the user should specify a negative seed. Note that if an |
---|
46 | object already exists the method will return that object |
---|
47 | and hence no new object is created. |
---|
48 | */ |
---|
49 | static random_singleton* get_instance( int seed = 0 ); |
---|
50 | |
---|
51 | |
---|
52 | /** |
---|
53 | A random unsgined long integer number is returned from a |
---|
54 | uniform distribution. |
---|
55 | |
---|
56 | @return If \a n is zero, or left out, a number between [ \a |
---|
57 | min, \a max ] is returned, otherwise a number between 0 and n-1 |
---|
58 | is returned. |
---|
59 | |
---|
60 | The values of \a min and \a max are obtained via |
---|
61 | get_uniform_int_min() and get_uniform_int_max(). |
---|
62 | */ |
---|
63 | inline u_long get_uniform_int(const u_long n=0) const; |
---|
64 | |
---|
65 | |
---|
66 | /** |
---|
67 | A random number (double precision) is returned |
---|
68 | from a uniform distribution between [ 0, 1 [. |
---|
69 | */ |
---|
70 | inline double get_uniform_double() const; |
---|
71 | |
---|
72 | |
---|
73 | /** |
---|
74 | A random number (double precision) is returned |
---|
75 | from a gaussioan distribution between where \a sigma |
---|
76 | can be specified by caller. \a sigma (\f$\sigma\f$) is |
---|
77 | the standard deviation in a gaussian distribution: \n\n |
---|
78 | \f$f(x) = \frac{1}{\sqrt{2\pi\sigma^2}}e^{-\frac{x^2}{2\sigma^2}}\f$ \n\n |
---|
79 | Default is \a sigma = 1. |
---|
80 | */ |
---|
81 | inline double get_gaussian( const double& sigma ) const; |
---|
82 | |
---|
83 | |
---|
84 | /** |
---|
85 | A random number (double precision) is returned |
---|
86 | from an exponential distribution. \a mu (\f$\mu\f$), the mean, |
---|
87 | can be specified by caller: \f$f(x) = \frac{1}{\mu}e^{-\frac{x}{\mu}}\f$. |
---|
88 | Default is \a mu = 1. |
---|
89 | */ |
---|
90 | inline double get_exponential( const double& mu ) const; |
---|
91 | |
---|
92 | |
---|
93 | /** |
---|
94 | See get_uniform_int()! |
---|
95 | */ |
---|
96 | inline u_long get_uniform_min() const; |
---|
97 | |
---|
98 | |
---|
99 | /** |
---|
100 | See get_uniform_int()! |
---|
101 | */ |
---|
102 | inline u_long get_uniform_max() const; |
---|
103 | |
---|
104 | |
---|
105 | /** |
---|
106 | Return seed to user for documentation purposes. |
---|
107 | */ |
---|
108 | inline int get_seed() const; |
---|
109 | |
---|
110 | /** |
---|
111 | Set a new seed |
---|
112 | */ |
---|
113 | void set_seed( const int& seed ); |
---|
114 | |
---|
115 | |
---|
116 | /** |
---|
117 | Set probabilities \a p for the general purpose |
---|
118 | distribution. |
---|
119 | */ |
---|
120 | inline void set_general_distribution_prob( const size_t& k, |
---|
121 | const double* p ); |
---|
122 | |
---|
123 | |
---|
124 | /** |
---|
125 | Get a random number (index) from the general purpose |
---|
126 | distribution |
---|
127 | */ |
---|
128 | inline size_t get_rnd_discrete(); |
---|
129 | |
---|
130 | |
---|
131 | /** |
---|
132 | GSL has different types of random genenerators. This function |
---|
133 | returns the one used. |
---|
134 | */ |
---|
135 | std::string get_generator_type(void) const; |
---|
136 | |
---|
137 | /// |
---|
138 | /// Operator for STL |
---|
139 | /// |
---|
140 | ///inline double operator()(void) { return get_uniform_double();} |
---|
141 | |
---|
142 | |
---|
143 | /** |
---|
144 | Destructor returning memory using GSL free functions |
---|
145 | */ |
---|
146 | ~random_singleton(); |
---|
147 | }; // random_singleton |
---|
148 | |
---|
149 | struct my_uniform_rng |
---|
150 | { |
---|
151 | public: |
---|
152 | inline u_long operator()(u_long i) { |
---|
153 | return random_singleton::get_instance()->get_uniform_int(i); |
---|
154 | } |
---|
155 | }; |
---|
156 | |
---|
157 | double random_singleton::get_exponential( const double& mu ) const |
---|
158 | { |
---|
159 | return gsl_ran_exponential( r_, mu ); |
---|
160 | } |
---|
161 | |
---|
162 | |
---|
163 | double random_singleton::get_gaussian( const double& sigma ) const |
---|
164 | { |
---|
165 | return gsl_ran_gaussian( r_, sigma ); |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | inline std::string random_singleton::get_generator_type() const |
---|
170 | { |
---|
171 | return static_cast<std::string>( gsl_rng_name( r_ ) ); |
---|
172 | } |
---|
173 | |
---|
174 | |
---|
175 | size_t random_singleton::get_rnd_discrete() |
---|
176 | { |
---|
177 | return gsl_ran_discrete( r_, gen_ ); |
---|
178 | } |
---|
179 | |
---|
180 | |
---|
181 | int random_singleton::get_seed() const |
---|
182 | { |
---|
183 | return gsl_rng_default_seed; |
---|
184 | } |
---|
185 | |
---|
186 | |
---|
187 | double random_singleton::get_uniform_double() const |
---|
188 | { |
---|
189 | return gsl_rng_uniform( r_ ); |
---|
190 | } |
---|
191 | |
---|
192 | |
---|
193 | u_long random_singleton::get_uniform_int(const u_long n) const |
---|
194 | { |
---|
195 | return ( n ? gsl_rng_uniform_int(r_,n) : gsl_rng_get( r_ )); |
---|
196 | } |
---|
197 | |
---|
198 | |
---|
199 | u_long random_singleton::get_uniform_min() const |
---|
200 | { |
---|
201 | return gsl_rng_min( r_ ); |
---|
202 | } |
---|
203 | |
---|
204 | |
---|
205 | u_long random_singleton::get_uniform_max() const |
---|
206 | { |
---|
207 | return gsl_rng_max( r_ ); |
---|
208 | } |
---|
209 | |
---|
210 | |
---|
211 | void random_singleton::set_general_distribution_prob( const size_t& k, |
---|
212 | const double* p ) |
---|
213 | { |
---|
214 | gen_ = gsl_ran_discrete_preproc( k, p ); |
---|
215 | } |
---|
216 | |
---|
217 | }} // of namespace utility and namespace theplu |
---|
218 | |
---|
219 | #endif |
---|