adevs
adevs_rand.h
1 
31 #ifndef __adevs_rand_h_
32 #define __adevs_rand_h_
33 #include "adevs.h"
34 #include <cstdlib>
35 
36 namespace adevs
37 {
38 
39 typedef enum
40 {
41  NDURATION,
42  NCOUNT,EMPTY,
43  NDELAY,
44  ADDTOQ,
45  EMPTYQ,
46  HNCLMS,
47  HMODE,
48  PROBVAL,
49  ERRUNIFORM,
50  ERRNORMAL,
51  ERRLOGNORM,
52  ERRTRIANG,
53  ERRGAMMA,
54  ERRBETA,
55  ERREXPONENT,
56  ERRERLANG,
57  ERRHYPGEO,
58  NULLEV,
59  NOHISTO,
60  INITERR,
61  AMODE,
62  HFORM,
63  ERRFILE,
64  SAMPLE,
65  FRACTION,
66  LEVEL,
67  SCAN,
68  SUPPRESS,
69  SEED
70 } errorType;
71 
76 class random_seq
77 {
78  public:
80  virtual void set_seed(unsigned long seed) = 0;
82  virtual double next_dbl() = 0;
84  virtual random_seq* copy() const = 0;
86  virtual unsigned long next_long() = 0;
88  virtual ~random_seq(){}
89 };
90 
97 class crand: public random_seq
98 {
99  public:
101  crand():seedp(0){}
103  crand(const crand& src):seedp(src.seedp){}
105  crand(unsigned long seed):seedp((unsigned int)seed){}
107  void set_seed(unsigned long seed) { seedp = (unsigned int)seed; }
109  double next_dbl()
110  {
111  return ((double)next_long()/(double)RAND_MAX);
112  }
114  unsigned long next_long();
116  random_seq* copy() const { return new crand(*this); }
118  ~crand(){}
119  private:
120  unsigned int seedp;
121 };
122 
127 class rv
128 {
129  public:
131  rv (unsigned long seed = 1);
136  rv(random_seq* rand);
138  rv(const rv& src);
140  const rv& operator=(const rv& src);
142  void set_seed(unsigned long seed);
144  unsigned long next_long();
147  double triangular(double a, double b, double c);
149  double uniform(double a, double b);
154  double normal(double m, double s);
161  double exponential(double a);
162  double hyperexponential(double p,double a,double b);
163  double laplace(double a);
164  double chisquare(unsigned int n);
165  double student(unsigned int n);
166  double lognormal(double a,double b);
167  double erlang(unsigned int n,double a);
168  double gamma(double a,double b);
169  double beta(double a,double b);
170  double fdistribution(unsigned int n,unsigned int m);
171  double poisson(double a);
172  double geometric(double p);
173  double hypergeometric(unsigned int m,unsigned int n,double p);
174  double weibull(double a,double b);
175  double binomial(double p,unsigned int n);
176  double negativebinomial(double p,unsigned int n);
177  double triangular(double a);
178  int probability(double p);
179  double lngamma(double xx);
181  ~rv();
182  private:
183  random_seq* _impl;
184  void err(errorType n);
185 };
186 
187 } // end of namespace
188 
189 #endif
190 
191 
virtual double next_dbl()=0
Get the next double uniformly distributed in [0, 1].
random_seq * copy() const
Copy the random number generator.
Definition: adevs_rand.h:116
~crand()
Destructor.
Definition: adevs_rand.h:118
~rv()
Destructor.
rv(unsigned long seed=1)
Create a random variable with the default implementation.
Definition: adevs_rand.h:76
crand(unsigned long seed)
Create a generator with the given seed.
Definition: adevs_rand.h:105
virtual random_seq * copy() const =0
Copy the random number generator.
virtual void set_seed(unsigned long seed)=0
Set the seed for the random number generator.
Definition: adevs_rand.h:127
crand(const crand &src)
Copy constructor.
Definition: adevs_rand.h:103
double uniform(double a, double b)
Sample a uniform distribution in the range [a, b].
void set_seed(unsigned long seed)
Set the seed for the random number generator.
Definition: adevs_rand.h:107
crand()
Create a generator with the default seed.
Definition: adevs_rand.h:101
double next_dbl()
Get the next double uniformly distributed in [0, 1].
Definition: adevs_rand.h:109
double normal(double m, double s)
Definition: adevs_rand.h:97
double exponential(double a)
unsigned long next_long()
Get a raw value from the underlying random number generator.
unsigned long next_long()
Get the next unsigned long.
virtual ~random_seq()
Destructor.
Definition: adevs_rand.h:88
virtual unsigned long next_long()=0
Get the next unsigned long.
const rv & operator=(const rv &src)
Assignment operator relies on copy method of underlying stream.
void set_seed(unsigned long seed)
See the random number generator implementation.
double triangular(double a, double b, double c)