random_gsl.h
Go to the documentation of this file.
1 /*
2  @copyright Russell Standish 2000-2013
3  @author Russell Standish
4  This file is part of EcoLab
5 
6  Open source licensed under the MIT license. See LICENSE for details.
7 */
8 
12 #include <gsl/gsl_rng.h>
13 #include <gsl/gsl_randist.h>
14 
15 namespace ecolab
16 {
17 
18  class urand: public random_gen
19  {
20  void operator=(urand&);
21  CLASSDESC_ACCESS(urand);
22  public:
23  gsl_rng *gen;
24  urand(const gsl_rng_type *descr=gsl_rng_mt19937) {gen=gsl_rng_alloc(descr);}
25  ~urand() {gsl_rng_free(gen);}
26  void Seed(int s) {gsl_rng_set(gen,s);}
27  void seed(TCL_args args) {Seed(args);}
28  double rand();
29  /* select a different uniform random generator according the GSL's rng
30  string interface */
31  void set_gen(TCL_args args);
32  void Set_gen(const string& descr)
33  {
34  static const gsl_rng_type ** rngTypes=gsl_rng_types_setup();
35  const gsl_rng_type **g=rngTypes;
36  for (; *g; ++g)
37  if (descr==(*g)->name)
38  {
39  gsl_rng_free(gen);
40  gen=gsl_rng_alloc(*g);
41  break;
42  }
43  if (!*g) throw error("Cannot create generator %s",descr.c_str());
44  }
45  };
46 
47  class gaussrand: public random_gen
48  {
49  void operator=(gaussrand&);
50  CLASSDESC_ACCESS(gaussrand);
51  public:
52  urand uni;
53  double rand();
54  };
55 }
56 
57  /* don't attempt to send these objects over the wire ! */
58 #ifdef CLASSDESC
59 #pragma omit pack ecolab::urand
60 #pragma omit unpack ecolab::urand
61 #endif
62 namespace classdesc_access
63 {
64  template <>
65  struct access_pack<ecolab::urand>:
66  public classdesc::NullDescriptor<classdesc::pack_t> {};
67  template <>
68  struct access_unpack<ecolab::urand>:
69  public classdesc::NullDescriptor<classdesc::unpack_t> {};
70  template <>
71  struct access_pack<ecolab::urand*>:
72  public classdesc::NullDescriptor<classdesc::pack_t> {};
73  template <>
74  struct access_unpack<ecolab::urand*>:
75  public classdesc::NullDescriptor<classdesc::unpack_t> {};
76 }
class to allow access to private members
Definition: classdesc_access.h:21
helper for constructing null descriptors
Definition: classdesc.h:784
class to allow access to private members
Definition: classdesc_access.h:22
#define CLASSDESC_ACCESS(type)
add friend statements for each accessor function
Definition: classdesc_access.h:36
_OPENMP
Definition: accessor.h:16
Contains access_* structs, and nothing else. These structs are used to gain access to private members...
Definition: accessor.h:55