source: trunk/lib/utility/random_singleton.h @ 312

Last change on this file since 312 was 312, checked in by Peter, 17 years ago

doc

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.8 KB
Line 
1// $Id: random_singleton.h 312 2005-05-12 07:57:20Z 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
14namespace theplu {
15namespace 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    /// @brief random number from Poisson distribution
95    ///
96    /// Probability that returned number i \f$ k \f$ is \f$ P(k) =
97    /// \frac{m^k}{k!}e^{-m} \f$
98    inline u_int get_poisson(const double m) const
99    { return gsl_ran_poisson(r_, m); }
100   
101    /**
102        @See get_uniform_int()!
103    */
104    inline u_long get_uniform_min() const;
105 
106
107    /**
108        @See get_uniform_int()!
109    */
110    inline u_long get_uniform_max() const;
111
112
113    /**
114       Return seed to user for documentation purposes.
115    */
116    inline int get_seed() const;
117
118    /**
119       Set a new seed
120    */
121    void set_seed( const int& seed );
122
123
124    /**
125       Set probabilities \a p for the general purpose
126       distribution.
127    */
128    inline void set_general_distribution_prob( const size_t& k, 
129                 const double* p );
130   
131
132    /**
133       Get a random number (index) from the general purpose
134       distribution
135    */
136    inline size_t get_rnd_discrete();
137
138
139    /**
140       GSL has different types of random genenerators. This function
141       returns the one used.
142    */
143    std::string get_generator_type(void) const;
144
145    ///
146    /// Operator for STL
147    ///
148    ///inline double operator()(void) { return get_uniform_double();}
149
150
151    /**
152       Destructor returning memory using GSL free functions
153    */
154    ~random_singleton();
155  }; // random_singleton
156 
157  struct my_uniform_rng
158  {
159  public:
160    inline u_long operator()(u_long i) {
161      return random_singleton::get_instance()->get_uniform_int(i);
162    }
163  };
164
165double random_singleton::get_exponential( const double& mu ) const
166{
167  return gsl_ran_exponential( r_, mu );
168}
169
170
171double random_singleton::get_gaussian( const double& sigma ) const
172{
173  return gsl_ran_gaussian( r_, sigma );
174}
175
176
177inline std::string random_singleton::get_generator_type() const
178{ 
179  return static_cast<std::string>( gsl_rng_name( r_ ) );
180}
181
182
183size_t random_singleton::get_rnd_discrete()
184{
185  return gsl_ran_discrete( r_, gen_ );
186}
187
188
189int random_singleton::get_seed() const
190{
191  return gsl_rng_default_seed;
192}
193
194
195double random_singleton::get_uniform_double() const
196{
197  return gsl_rng_uniform( r_ );
198}
199
200
201u_long random_singleton::get_uniform_int(const u_long n) const
202{
203    return ( n ? gsl_rng_uniform_int(r_,n) : gsl_rng_get( r_ ));
204}
205
206
207u_long random_singleton::get_uniform_min() const
208{
209  return gsl_rng_min( r_ );
210}
211
212
213u_long random_singleton::get_uniform_max() const
214{
215  return gsl_rng_max( r_ );
216}
217
218
219void random_singleton::set_general_distribution_prob( const size_t& k, 
220                  const double* p )
221{
222  gen_ = gsl_ran_discrete_preproc( k, p ); 
223}
224
225}} // of namespace utility and namespace theplu
226
227#endif
Note: See TracBrowser for help on using the repository browser.