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