KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsl_rng.h
Go to the documentation of this file.
1 
6 /*
7  * $Id: gsl_rng.h 17393 2014-08-23 20:19:14Z predoehl $
8  */
9 
10 #ifndef GSL_RNG_H_INCLUDED_LIBKJB_UOFARIZONAVISION
11 #define GSL_RNG_H_INCLUDED_LIBKJB_UOFARIZONAVISION
12 
13 #include <l_cpp/l_exception.h>
14 #include <gsl_cpp/gsl_util.h>
15 #include <vector>
16 #include <string>
17 
18 #ifdef KJB_HAVE_GSL
19 #include "gsl/gsl_rng.h" /* this is a GSL header, not our wrapper */
20 #else
21 #warning "Compiling GNU GSL wrapper without GNU GSL; it will not run properly"
22 typedef void gsl_rng;
23 typedef void gsl_rng_type;
30  *gsl_rng_cmrg,
31  *gsl_rng_mrg,
34 #endif
35 
36 
37 namespace kjb {
38 
39 
47 enum {
58 };
59 
60 
61 #ifdef KJB_HAVE_GSL
62 std::string gsl_rng_serialize_implementation( const gsl_rng* );
63 
64 void gsl_rng_deserialize_implementation( gsl_rng*, const std::string& );
65 #endif
66 
67 
85 template< unsigned KIND >
87 
88  gsl_rng *m_rng;
89 
90  // I assume there will be fewer than, say, 100 kinds of RNG.
91  static char kind()
92  {
93  return char( '0' + KIND );
94  }
95 
96 public:
97 
99  Gsl_rng_basic( const gsl_rng_type* type )
100 #ifdef KJB_HAVE_GSL
101  : m_rng( gsl_rng_alloc( type ) )
102  {
103  ETX_2( 00 == m_rng, "Gsl_rng_basic ctor: bad_alloc" );
104  }
105 #else
106  {
107  KJB_THROW_2( Missing_dependency, "GNU GSL" );
108  }
109 #endif
110 
113 #ifdef KJB_HAVE_GSL
114  : m_rng( gsl_rng_clone( rng.m_rng ) )
115  {
116  ETX_2( 00 == m_rng, "Gsl_rng_basic copy ctor: bad_alloc" );
117  }
118 #else
119  {
120  KJB_THROW_2( Missing_dependency, "GNU GSL" );
121  }
122 #endif
123 
126  {
127  if ( this != &that )
128  {
129 #ifdef KJB_HAVE_GSL
130  gsl_rng_memcpy( m_rng, that.m_rng );
131 #endif
132  }
133  return *this;
134  }
135 
137  void seed( unsigned long seed_val ) const
138  {
139 #ifdef KJB_HAVE_GSL
140  gsl_rng_set( m_rng, seed_val );
141 #endif
142  }
143 
145  std::string serialize() const
146  {
147 #ifdef KJB_HAVE_GSL
148  return gsl_rng_serialize_implementation( m_rng ) + kind();
149 #endif
150  }
151 
159  void deserialize( const std::string& state ) const
160  {
161 #ifdef KJB_HAVE_GSL
162  if ( state.size() < 2 || state[ state.size() - 1 ] != kind() )
163  {
164  GSL_ETX( GSL_EFAILED );
165  }
166 
167  gsl_rng_deserialize_implementation( m_rng,
168  std::string( state.begin(), state.end() - 1 ) );
169 #endif
170  }
171 
177  unsigned long get() const
178  {
179 #ifdef KJB_HAVE_GSL
180  return gsl_rng_get( m_rng );
181 #endif
182  }
183 
185  const char* name() const
186  {
187 #ifdef KJB_HAVE_GSL
188  return gsl_rng_name( m_rng );
189 #endif
190  }
191 
193  unsigned long max() const
194  {
195 #ifdef KJB_HAVE_GSL
196  return gsl_rng_max( m_rng );
197 #endif
198  }
199 
201  unsigned long min() const
202  {
203 #ifdef KJB_HAVE_GSL
204  return gsl_rng_min( m_rng );
205 #endif
206  }
207 
209  double uniform() const
210  {
211 #ifdef KJB_HAVE_GSL
212  return gsl_rng_uniform( m_rng );
213 #endif
214  }
215 
217  double uniform_pos() const
218  {
219 #ifdef KJB_HAVE_GSL
220  return gsl_rng_uniform_pos( m_rng );
221 #endif
222  }
223 
229  unsigned long uniform_int( unsigned long end_value ) const
230  {
231 #ifdef KJB_HAVE_GSL
232  unsigned long dev = gsl_rng_uniform_int( m_rng, end_value );
233 
234  if ( 0 == dev && end_value > max() )
235  {
236  GSL_ETX( GSL_EINVAL );
237  }
238 
239  return dev;
240 #endif
241  }
242 
244  operator const gsl_rng*() const
245  {
246  return m_rng;
247  }
248 
249 #ifdef KJB_HAVE_GSL
250  ~Gsl_rng_basic()
252  {
253  gsl_rng_free( m_rng );
254  }
255 #endif
256 
257 };
258 
259 
261 #define Gsl_rng_template( Foo, foo, FOO ) \
262  struct Foo : public Gsl_rng_basic< FOO > \
263  { \
264  Foo() : Gsl_rng_basic< FOO >( foo ) {} \
265  }
266 
277 
290 
303 
316 
329 
342 
352 
362 
372 
381 
382 
398 
399 } // end ns kjb
400 
401 #endif /* GSL_RNG_H_INCLUDED_LIBKJB_UOFARIZONAVISION */
Matsumoto's & Nishimura's "Mersenne Twister".
Definition: gsl_rng.h:48
void gsl_rng
Definition: gsl_rng.h:22
unsigned long min() const
minimum possible value that get() can potentially return.
Definition: gsl_rng.h:201
const char * name() const
return C-style string of the name of the generator algorithm.
Definition: gsl_rng.h:185
void gsl_rng_type
Definition: gsl_rng.h:23
Random number generator using the "RANLUX" algorithm, 24 bits. This implements the "RANLUX" algor...
Random number generator using a four-tap XOR using a shift register. This uses Ziff's offsets (1998) ...
RANLUX double-precision level 2.
Definition: gsl_rng.h:53
unsigned long uniform_int(unsigned long end_value) const
Sample uniformly dist. non-neg. integers less than end_value.
Definition: gsl_rng.h:229
void seed(unsigned long seed_val) const
seed the generator to determine its future values
Definition: gsl_rng.h:137
GSL utility stuff to help those using the C++ wrapper on GSL code.
gsl_rng_type * gsl_rng_gfsr4
Definition: gsl_rng.h:24
gsl_rng_type * gsl_rng_cmrg
Definition: gsl_rng.h:24
Random number generator using the "Mersenne Twister" algorithm. This implements the "Mersenne Twister...
void deserialize(const std::string &state) const
return generator to the state "snapshot" from serialize().
Definition: gsl_rng.h:159
Random number generator using the "RANLUX" algorithm, 48 bits, lvl. 1 This implements the "RANLUX"...
unsigned long max() const
maximum possible value that get() can potentially return.
Definition: gsl_rng.h:193
Random number generator using L'Ecuyer's 1996 algorithm. This implements the Combined Multiple Recurs...
RANLUX single-precision level 0.
Definition: gsl_rng.h:49
gsl_rng_type * gsl_rng_ranlxd2
Definition: gsl_rng.h:24
Gsl_rng_template(Gsl_rng_mt19937, gsl_rng_mt19937, GSL_RNG_MT19937)
Gsl_rng_mt19937 Gsl_rng_default
An all-around good, fast, simulation-quality random number generator.
Definition: gsl_rng.h:397
gsl_rng_type * gsl_rng_ranlxs0
Definition: gsl_rng.h:24
Gsl_rng_basic(const gsl_rng_type *type)
ctor defines type – not meant to be called directly by users.
Definition: gsl_rng.h:99
double uniform_pos() const
Sample uniformly distributed positive float in interval (0,1)
Definition: gsl_rng.h:217
Random number generator using the "RANLUX" algorithm, 24 bits. This implements the "RANLUX" algor...
#define GSL_ETX(gsl_expr)
Definition: gsl_util.h:37
Four-tap-XOR shift generator.
Definition: gsl_rng.h:57
Random number generator using the "RANLUX" algorithm, 48 bits, lvl. 1 This implements the "RANLUX"...
RANLUX single-precision level 2.
Definition: gsl_rng.h:51
Random number generator using 1993 algorithm of L'Ecuyer et al. This implements the Multiple Recursiv...
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
std::string serialize() const
take a snapshot of the generator state right now
Definition: gsl_rng.h:145
double uniform() const
Sample uniformly distributed float in interval [0,1)
Definition: gsl_rng.h:209
Gsl_rng_basic< KIND > & operator=(const Gsl_rng_basic< KIND > &that)
assignment operator from an RNG of the same kind
Definition: gsl_rng.h:125
#define ETX_2(a, msg)
Definition: l_exception.h:78
RANLUX single-precision level 1.
Definition: gsl_rng.h:50
gsl_rng_type * gsl_rng_mt19937
Definition: gsl_rng.h:24
Random number generator using Tausworthe's algorithm. This is L'Ecuyer's version of Tausworthe's algo...
RANLUX double-precision level 1.
Definition: gsl_rng.h:52
Tausworthe generator version 2.
Definition: gsl_rng.h:56
Support for error handling exception classes in libKJB.
Object thrown when a program lacks required resources or libraries.
Definition: l_exception.h:539
gsl_rng_type * gsl_rng_taus2
Definition: gsl_rng.h:24
L'Ecuyer's '96 generator, period ~ 10^56.
Definition: gsl_rng.h:54
Random number generator using the "RANLUX" algorithm, 24 bits. This implements the "RANLUX" algor...
gsl_rng_type * gsl_rng_ranlxs1
Definition: gsl_rng.h:24
gsl_rng_type * gsl_rng_ranlxs2
Definition: gsl_rng.h:24
L'Ecuyer et al. '93 generator, period ~ 10^46.
Definition: gsl_rng.h:55
gsl_rng_type * gsl_rng_mrg
Definition: gsl_rng.h:24
Basic RAII wrapper for GNU GSL random number generators.
Definition: gsl_rng.h:86
gsl_rng_type * gsl_rng_ranlxd1
Definition: gsl_rng.h:24