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  #ifdef _WIN32
112  return ((double)next_long()/(double)UINT_MAX);
113  #else
114  return ((double)next_long()/(double)RAND_MAX);
115  #endif
116  }
118  unsigned long next_long();
120  random_seq* copy() const { return new crand(*this); }
122  ~crand(){}
123  private:
124  unsigned int seedp;
125 };
126 
131 class rv
132 {
133  public:
135  rv (unsigned long seed = 1);
140  rv(random_seq* rand);
142  rv(const rv& src);
144  const rv& operator=(const rv& src);
146  void set_seed(unsigned long seed);
148  unsigned long next_long();
151  double triangular(double a, double b, double c);
153  double uniform(double a, double b);
158  double normal(double m, double s);
165  double exponential(double a);
166  double hyperexponential(double p,double a,double b);
167  double laplace(double a);
168  double chisquare(unsigned int n);
169  double student(unsigned int n);
170  double lognormal(double a,double b);
171  double erlang(unsigned int n,double a);
172  double gamma(double a,double b);
173  double beta(double a,double b);
174  double fdistribution(unsigned int n,unsigned int m);
175  double poisson(double a);
176  double geometric(double p);
177  double hypergeometric(unsigned int m,unsigned int n,double p);
178  double weibull(double a,double b);
179  double binomial(double p,unsigned int n);
180  double negativebinomial(double p,unsigned int n);
181  double triangular(double a);
182  int probability(double p);
183  double lngamma(double xx);
185  ~rv();
186  private:
187  random_seq* _impl;
188  void err(errorType n);
189 };
190 
191 } // end of namespace
192 
193 #endif
194 
195 
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:120
~crand()
Destructor.
Definition: adevs_rand.h:122
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:131
crand(const crand &src)
Copy constructor.
Definition: adevs_rand.h:103
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
Definition: adevs_fmi.h:56
double next_dbl()
Get the next double uniformly distributed in [0, 1].
Definition: adevs_rand.h:109
Definition: adevs_rand.h:97
virtual ~random_seq()
Destructor.
Definition: adevs_rand.h:88
virtual unsigned long next_long()=0
Get the next unsigned long.