random.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 
10 #ifndef RANDOM_H
11 #define RANDOM_H
12 
13 #ifdef MPI_SUPPORT
14 #include <mpi.h>
15 #endif
16 
17 #include <string>
18 
19 #include "classdesc_access.h"
20 #include "pack_base.h"
21 #include "pack_stl.h"
22 #include "pack_graph.h"
23 #include "TCL_obj_base.h"
24 
25 #include <stdlib.h>
26 #include <math.h>
27 
28 namespace ecolab
29 {
30 
32  class random_gen
33  {
34  public:
35  virtual double rand()=0;
36  virtual ~random_gen() {}
37  };
38 }
39 
40 
41 #ifdef UNURAN
42 #include "random_unuran.h"
43 #elif defined(GNUSL)
44 #include "random_gsl.h"
45 #else
46 #include "random_basic.h"
47 #endif
48 
49 namespace ecolab
50 {
52  class affinerand: public random_gen
53  {
54  random_gen *gen;
55  bool allocated;
57  void del_gen() {if (allocated) delete gen;}
58  public:
59  double scale, offset;
60  affinerand(double s=1, double o=0):
61  scale(s), offset(o) {gen=new urand; allocated=true;}
62  affinerand(double s, double o, random_gen *g):
63  scale(s), offset(o) {Set_gen(g);}
64  ~affinerand() {del_gen();}
65  void Set_gen(random_gen *g) {del_gen(); gen=g; allocated=false;}
66  template <class T> void new_gen(const T& g)
67  {del_gen(); gen=new T; allocated=true;}
68  void set_gen(TCL_args);
69  double rand();
70  };
71 
76  class distrand: public random_gen
77  {
78  std::vector<double> P;
79  std::vector<int> PP;
80  std::vector<double> a;
81  int Pwidth;
82  std::vector<double> pbase;
83  int base;
84  int delta(double x, int i) {return int(pbase[i]*x) % base;}
85  int trunctowidth(double x,int w) {return int(pbase[w]*x);}
86  urand uniform;
87  /* ith hexadecimal digit of x */
88  double pow(int x, int y) {return ::pow((double)x,y);}
90  public:
91  int nsamp; /* no. of sample points in distribution */
92  int width; /* digits of precision (base 16) used from prob. distribution */
93  double min, max; /* distribution endpoints */
94  distrand():base(10) {nsamp=10; width=3; min=0; max=1;}
95  void Init(int argc, char *argv[]);
96  void init(double (*f)(double));
97  double rand();
98  };
99 
100 } // namespace ecolab
101 
102 inline void pack(classdesc::pack_t& t, const classdesc::string& d,
103  ecolab::random_gen*& a) {}
104 inline void unpack(classdesc::unpack_t& t, const classdesc::string& d,
105  ecolab::random_gen*& a) {}
106 
107 #include "random.cd"
108 
109 #ifdef UNURAN
110 #include "random_unuran.cd"
111 #elif defined(GNUSL)
112 #include "random_gsl.cd"
113 #else
114 #include "random_basic.cd"
115 #endif
116 
117 #endif /* RANDOM_H */
descriptor access to a class&#39;s privates
random generator based on the UNURAN library
arbitrary distribution generator (Marsaglia method).
Definition: random.h:76
serialisation descriptor
abstract base class for representing random number generators
Definition: random.h:32
#define CLASSDESC_ACCESS(type)
add friend statements for each accessor function
Definition: classdesc_access.h:36
Represent arguments to TCL commands.
Definition: TCL_obj_base.h:138
random generator based on GNUSL implementation
TCL access descriptor.
serialisation for standard containers
scale and translate a random number generator
Definition: random.h:52
Definition: TCL_obj_base.h:124
_OPENMP
Definition: accessor.h:16
serialisation for dynamic structures (graphs/trees and so on)
Definition: random_basic.h:11