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