OMNeT++ Simulation Library  5.6.1
crandom.h
1 //==========================================================================
2 // CRANDOM.H - part of
3 // OMNeT++/OMNEST
4 // Discrete System Simulation in C++
5 //
6 //==========================================================================
7 
8 /*--------------------------------------------------------------*
9  Copyright (C) 1992-2017 Andras Varga
10  Copyright (C) 2006-2017 OpenSim Ltd.
11 
12  This file is distributed WITHOUT ANY WARRANTY. See the file
13  `license' for details on this and other legal matters.
14 *--------------------------------------------------------------*/
15 
16 #ifndef __OMNETPP_CRANDOM_H
17 #define __OMNETPP_CRANDOM_H
18 
19 #include <sstream>
20 #include "cownedobject.h"
21 
22 namespace omnetpp {
23 
24 class cRNG;
25 
31 class SIM_API cRandom : public cOwnedObject
32 {
33  protected:
34  cRNG *rng;
35 
36  public:
39  cRandom(cRNG *rng);
40  cRandom(const char *name=nullptr, cRNG *rng=nullptr);
41  virtual ~cRandom() {}
43 
49  virtual void setRNG(cRNG *rng) {this->rng = rng;}
50 
54  cRNG *getRNG() const {return rng;}
56 
62  virtual double draw() const = 0;
64 };
65 
75 class SIM_API cUniform : public cRandom
76 {
77  protected:
78  double a;
79  double b;
80  private:
81  void copy(const cUniform& other);
82  public:
85  cUniform(cRNG *rng, double a, double b) : cRandom(rng), a(a), b(b) {}
86  cUniform(const char *name=nullptr, cRNG *rng=nullptr, double a=0, double b=1) : cRandom(name, rng), a(a), b(b) {}
87  cUniform(const cUniform& other) : cRandom(other) {copy(other);}
88  virtual cUniform *dup() const override {return new cUniform(*this);}
89  cUniform& operator =(const cUniform& other);
90  virtual std::string str() const override;
92 
95  void setA(double a) {this->a = a;}
96  double getA() const {return a;}
97  void setB(double b) {this->b = b;}
98  double getB() const {return b;}
100 
103  virtual double draw() const override;
105 };
106 
115 class SIM_API cExponential : public cRandom
116 {
117  protected:
118  double mean;
119  private:
120  void copy(const cExponential& other);
121  public:
124  cExponential(cRNG *rng, double mean) : cRandom(rng), mean(mean) {}
125  cExponential(const char *name=nullptr, cRNG *rng=nullptr, double mean=1) : cRandom(rng), mean(mean) {}
126  cExponential(const cExponential& other) : cRandom(other) {copy(other);}
127  virtual cExponential *dup() const override {return new cExponential(*this);}
128  const cExponential& operator =(const cExponential& other);
129  virtual std::string str() const override;
131 
134  void setMean(double mean) {this->mean = mean;}
135  double getMean() const {return mean;}
137 
140  virtual double draw() const override;
142 };
143 
153 class SIM_API cNormal : public cRandom
154 {
155  protected:
156  double mean;
157  double stddev;
158  private:
159  void copy(const cNormal& other);
160  public:
163  cNormal(cRNG *rng, double mean, double stddev) : cRandom(rng), mean(mean), stddev(stddev) {}
164  cNormal(const char *name=nullptr, cRNG *rng=nullptr, double mean=0, double stddev=1) : cRandom(rng), mean(mean), stddev(stddev) {}
165  cNormal(const cNormal& other) : cRandom(other) {copy(other);}
166  virtual cNormal *dup() const override {return new cNormal(*this);}
167  cNormal& operator =(const cNormal& other);
168  virtual std::string str() const override;
170 
173  void setMean(double mean) {this->mean = mean;}
174  double getMean() const {return mean;}
175  void setStddev(double stddev) {this->stddev = stddev;}
176  double getStddev() const {return stddev;}
178 
181  virtual double draw() const override;
183 };
184 
194 class SIM_API cTruncNormal : public cRandom
195 {
196  protected:
197  double mean;
198  double stddev;
199  private:
200  void copy(const cTruncNormal& other);
201  public:
204  cTruncNormal(cRNG *rng, double mean, double stddev) : cRandom(rng), mean(mean), stddev(stddev) {}
205  cTruncNormal(const char *name=nullptr, cRNG *rng=nullptr, double mean=0, double stddev=1) : cRandom(rng), mean(mean), stddev(stddev) {}
206  cTruncNormal(const cTruncNormal& other) : cRandom(other) {copy(other);}
207  virtual cTruncNormal *dup() const override {return new cTruncNormal(*this);}
208  cTruncNormal& operator =(const cTruncNormal& other);
209  virtual std::string str() const override;
211 
214  void setMean(double mean) {this->mean = mean;}
215  double getMean() const {return mean;}
216  void setStddev(double stddev) {this->stddev = stddev;}
217  double getStddev() const {return stddev;}
219 
222  virtual double draw() const override;
224 };
225 
235 class SIM_API cGamma : public cRandom
236 {
237  protected:
238  double alpha;
239  double theta;
240  private:
241  void copy(const cGamma& other);
242  public:
245  cGamma(cRNG *rng, double alpha, double theta) : cRandom(rng), alpha(alpha), theta(theta) {}
246  cGamma(const char *name=nullptr, cRNG *rng=nullptr, double alpha=1, double theta=1) : cRandom(rng), alpha(alpha), theta(theta) {}
247  cGamma(const cGamma& other) : cRandom(other) {copy(other);}
248  virtual cGamma *dup() const override {return new cGamma(*this);}
249  cGamma& operator =(const cGamma& other);
250  virtual std::string str() const override;
252 
255  void setAlpha(double alpha) {this->alpha = alpha;}
256  double getAlpha() const {return alpha;}
257  void setTheta(double theta) {this->theta = theta;}
258  double getTheta() const {return theta;}
260 
263  virtual double draw() const override;
265 };
266 
276 class SIM_API cBeta : public cRandom
277 {
278  protected:
279  double alpha1;
280  double alpha2;
281  private:
282  void copy(const cBeta& other);
283  public:
286  cBeta(cRNG *rng, double alpha1, double alpha2) : cRandom(rng), alpha1(alpha1), alpha2(alpha2) {}
287  cBeta(const char *name=nullptr, cRNG *rng=nullptr, double alpha1=1, double alpha2=1) : cRandom(rng), alpha1(alpha1), alpha2(alpha2) {}
288  cBeta(const cBeta& other) : cRandom(other) {copy(other);}
289  virtual cBeta *dup() const override {return new cBeta(*this);}
290  cBeta& operator =(const cBeta& other);
291  virtual std::string str() const override;
293 
296  void setAlpha1(double alpha1) {this->alpha1 = alpha1;}
297  double getAlpha1() const {return alpha1;}
298  void setAlpha2(double alpha2) {this->alpha2 = alpha2;}
299  double getAlpha2() const {return alpha2;}
301 
304  virtual double draw() const override;
306 };
307 
317 class SIM_API cErlang : public cRandom
318 {
319  protected:
320  unsigned int k;
321  double mean;
322  private:
323  void copy(const cErlang& other);
324  public:
327  cErlang(cRNG *rng, unsigned int k, double mean) : cRandom(rng), k(k), mean(mean) {}
328  cErlang(const char *name=nullptr, cRNG *rng=nullptr, unsigned int k=1, double mean=1) : cRandom(rng), k(k), mean(mean) {}
329  cErlang(const cErlang& other) : cRandom(other) {copy(other);}
330  virtual cErlang *dup() const override {return new cErlang(*this);}
331  cErlang& operator =(const cErlang& other);
332  virtual std::string str() const override;
334 
337  void setK(unsigned int k) {this->k = k;}
338  unsigned int getK() const {return k;}
339  void setMean(double mean) {this->mean = mean;}
340  double getMean() const {return mean;}
342 
345  virtual double draw() const override;
347 };
348 
358 class SIM_API cChiSquare : public cRandom
359 {
360  protected:
361  unsigned int k;
362  private:
363  void copy(const cChiSquare& other);
364  public:
367  cChiSquare(cRNG *rng, unsigned int k) : cRandom(rng), k(k) {}
368  cChiSquare(const char *name=nullptr, cRNG *rng=nullptr, unsigned int k=1) : cRandom(rng), k(k) {}
369  cChiSquare(const cChiSquare& other) : cRandom(other) {copy(other);}
370  virtual cChiSquare *dup() const override {return new cChiSquare(*this);}
371  cChiSquare& operator =(const cChiSquare& other);
372  virtual std::string str() const override;
374 
377  void setK(unsigned int k) {this->k = k;}
378  unsigned int getK() const {return k;}
380 
383  virtual double draw() const override;
385 };
386 
396 class SIM_API cStudentT : public cRandom
397 {
398  protected:
399  unsigned int i;
400  private:
401  void copy(const cStudentT& other);
402  public:
405  cStudentT(cRNG *rng, unsigned int i) : cRandom(rng), i(i) {}
406  cStudentT(const char *name=nullptr, cRNG *rng=nullptr, unsigned int i=1) : cRandom(rng), i(i) {}
407  cStudentT(const cStudentT& other) : cRandom(other) {copy(other);}
408  virtual cStudentT *dup() const override {return new cStudentT(*this);}
409  cStudentT& operator =(const cStudentT& other);
410  virtual std::string str() const override;
412 
415  void setI(unsigned int i) {this->i = i;}
416  unsigned int getI() const {return i;}
418 
421  virtual double draw() const override;
423 };
424 
434 class SIM_API cCauchy : public cRandom
435 {
436  protected:
437  double a;
438  double b;
439  private:
440  void copy(const cCauchy& other);
441  public:
444  cCauchy(cRNG *rng, double a, double b) : cRandom(rng), a(a), b(b) {}
445  cCauchy(const char *name=nullptr, cRNG *rng=nullptr, double a=0, double b=1) : cRandom(rng), a(a), b(b) {}
446  cCauchy(const cCauchy& other) : cRandom(other) {copy(other);}
447  virtual cCauchy *dup() const override {return new cCauchy(*this);}
448  cCauchy& operator =(const cCauchy& other);
449  virtual std::string str() const override;
451 
454  void setA(double a) {this->a = a;}
455  double getA() const {return a;}
456  void setB(double b) {this->b = b;}
457  double getB() const {return b;}
459 
462  virtual double draw() const override;
464 };
465 
475 class SIM_API cTriang : public cRandom
476 {
477  protected:
478  double a;
479  double b;
480  double c;
481  private:
482  void copy(const cTriang& other);
483  public:
486  cTriang(cRNG *rng, double a, double b, double c) : cRandom(rng), a(a), b(b), c(c) {}
487  cTriang(const char *name=nullptr, cRNG *rng=nullptr, double a=-1, double b=0, double c=1) : cRandom(rng), a(a), b(b), c(c) {}
488  cTriang(const cTriang& other) : cRandom(other) {copy(other);}
489  virtual cTriang *dup() const override {return new cTriang(*this);}
490  cTriang& operator =(const cTriang& other);
491  virtual std::string str() const override;
493 
496  void setA(double a) {this->a = a;}
497  double getA() const {return a;}
498  void setB(double b) {this->b = b;}
499  double getB() const {return b;}
500  void setC(double c) {this->c = c;}
501  double getC() const {return c;}
503 
506  virtual double draw() const override;
508 };
509 
519 class SIM_API cWeibull : public cRandom
520 {
521  protected:
522  double a;
523  double b;
524  private:
525  void copy(const cWeibull& other);
526  public:
529  cWeibull(cRNG *rng, double a, double b) : cRandom(rng), a(a), b(b) {}
530  cWeibull(const char *name=nullptr, cRNG *rng=nullptr, double a=1, double b=1) : cRandom(rng), a(a), b(b) {}
531  cWeibull(const cWeibull& other) : cRandom(other) {copy(other);}
532  virtual cWeibull *dup() const override {return new cWeibull(*this);}
533  cWeibull& operator =(const cWeibull& other);
534  virtual std::string str() const override;
536 
539  void setA(double a) {this->a = a;}
540  double getA() const {return a;}
541  void setB(double b) {this->b = b;}
542  double getB() const {return b;}
544 
547  virtual double draw() const override;
549 };
550 
560 class SIM_API cParetoShifted : public cRandom
561 {
562  protected:
563  double a;
564  double b;
565  double c;
566  private:
567  void copy(const cParetoShifted& other);
568  public:
571  cParetoShifted(cRNG *rng, double a, double b, double c) : cRandom(rng), a(a), b(b), c(c) {}
572  cParetoShifted(const char *name=nullptr, cRNG *rng=nullptr, double a=1, double b=1, double c=0) : cRandom(rng), a(a), b(b), c(c) {}
573  cParetoShifted(const cParetoShifted& other) : cRandom(other) {copy(other);}
574  virtual cParetoShifted *dup() const override {return new cParetoShifted(*this);}
575  cParetoShifted& operator =(const cParetoShifted& other);
576  virtual std::string str() const override;
578 
581  void setA(double a) {this->a = a;}
582  double getA() const {return a;}
583  void setB(double b) {this->b = b;}
584  double getB() const {return b;}
585  void setC(double c) {this->c = c;}
586  double getC() const {return c;}
588 
591  virtual double draw() const override;
593 };
594 
595 // discrete:
596 
606 class SIM_API cIntUniform : public cRandom
607 {
608  protected:
609  int a;
610  int b;
611  private:
612  void copy(const cIntUniform& other);
613  public:
616  cIntUniform(cRNG *rng, int a, int b) : cRandom(rng), a(a), b(b) {}
617  cIntUniform(const char *name=nullptr, cRNG *rng=nullptr, int a=0, int b=1) : cRandom(rng), a(a), b(b) {}
618  cIntUniform(const cIntUniform& other) : cRandom(other) {copy(other);}
619  virtual cIntUniform *dup() const override {return new cIntUniform(*this);}
620  cIntUniform& operator =(const cIntUniform& other);
621  virtual std::string str() const override;
623 
626  void setA(int a) {this->a = a;}
627  int getA() const {return a;}
628  void setB(int b) {this->b = b;}
629  int getB() const {return b;}
631 
634  virtual double draw() const override;
636 };
637 
647 class SIM_API cIntUniformExcl : public cRandom
648 {
649  protected:
650  int a;
651  int b;
652  private:
653  void copy(const cIntUniformExcl& other);
654  public:
657  cIntUniformExcl(cRNG *rng, int a, int b) : cRandom(rng), a(a), b(b) {}
658  cIntUniformExcl(const char *name=nullptr, cRNG *rng=nullptr, int a=0, int b=1) : cRandom(rng), a(a), b(b) {}
659  cIntUniformExcl(const cIntUniformExcl& other) : cRandom(other) {copy(other);}
660  virtual cIntUniformExcl *dup() const override {return new cIntUniformExcl(*this);}
661  cIntUniformExcl& operator =(const cIntUniformExcl& other);
662  virtual std::string str() const override;
664 
667  void setA(int a) {this->a = a;}
668  int getA() const {return a;}
669  void setB(int b) {this->b = b;}
670  int getB() const {return b;}
672 
675  virtual double draw() const override;
677 };
678 
688 class SIM_API cBernoulli : public cRandom
689 {
690  protected:
691  double p;
692  private:
693  void copy(const cBernoulli& other);
694  public:
697  cBernoulli(cRNG *rng, double p) : cRandom(rng), p(p) {}
698  cBernoulli(const char *name=nullptr, cRNG *rng=nullptr, double p=0.5) : cRandom(rng), p(p) {}
699  cBernoulli(const cBernoulli& other) : cRandom(other) {copy(other);}
700  virtual cBernoulli *dup() const override {return new cBernoulli(*this);}
701  cBernoulli& operator =(const cBernoulli& other);
702  virtual std::string str() const override;
704 
707  void setP(double p) {this->p = p;}
708  double getP() const {return p;}
710 
713  virtual double draw() const override;
715 };
716 
726 class SIM_API cBinomial : public cRandom
727 {
728  protected:
729  int n;
730  double p;
731  private:
732  void copy(const cBinomial& other);
733  public:
736  cBinomial(cRNG *rng, int n, double p) : cRandom(rng), n(n), p(p) {}
737  cBinomial(const char *name=nullptr, cRNG *rng=nullptr, int n=1, double p=0.5) : cRandom(rng), n(n), p(p) {}
738  cBinomial(const cBinomial& other) : cRandom(other) {copy(other);}
739  virtual cBinomial *dup() const override {return new cBinomial(*this);}
740  cBinomial& operator =(const cBinomial& other);
741  virtual std::string str() const override;
743 
746  void setN(int n) {this->n = n;}
747  int getN() const {return n;}
748  void setP(double p) {this->p = p;}
749  double getP() const {return p;}
751 
754  virtual double draw() const override;
756 };
757 
767 class SIM_API cGeometric : public cRandom
768 {
769  protected:
770  double p;
771  private:
772  void copy(const cGeometric& other);
773  public:
776  cGeometric(cRNG *rng, double p) : cRandom(rng), p(p) {}
777  cGeometric(const char *name=nullptr, cRNG *rng=nullptr, double p=0.5) : cRandom(rng), p(p) {}
778  cGeometric(const cGeometric& other) : cRandom(other) {copy(other);}
779  virtual cGeometric *dup() const override {return new cGeometric(*this);}
780  cGeometric& operator =(const cGeometric& other);
781  virtual std::string str() const override;
783 
786  void setP(double p) {this->p = p;}
787  double getP() const {return p;}
789 
792  virtual double draw() const override;
794 };
795 
805 class SIM_API cNegBinomial : public cRandom
806 {
807  protected:
808  int n;
809  double p;
810  private:
811  void copy(const cNegBinomial& other);
812  public:
815  cNegBinomial(cRNG *rng, int n, double p) : cRandom(rng), n(n), p(p) {}
816  cNegBinomial(const char *name=nullptr, cRNG *rng=nullptr, int n=0, double p=0.5) : cRandom(rng), n(n), p(p) {}
817  cNegBinomial(const cNegBinomial& other) : cRandom(other) {copy(other);}
818  virtual cNegBinomial *dup() const override {return new cNegBinomial(*this);}
819  cNegBinomial& operator =(const cNegBinomial& other);
820  virtual std::string str() const override;
822 
825  void setN(int n) {this->n = n;}
826  int getN() const {return n;}
827  void setP(double p) {this->p = p;}
828  double getP() const {return p;}
830 
833  virtual double draw() const override;
835 };
836 
846 class SIM_API cPoisson : public cRandom
847 {
848  protected:
849  double lambda;
850  private:
851  void copy(const cPoisson& other);
852  public:
855  cPoisson(cRNG *rng, double lambda) : cRandom(rng), lambda(lambda) {}
856  cPoisson(const char *name=nullptr, cRNG *rng=nullptr, double lambda=1) : cRandom(rng), lambda(lambda) {}
857  cPoisson(const cPoisson& other) : cRandom(other) {copy(other);}
858  virtual cPoisson *dup() const override {return new cPoisson(*this);}
859  cPoisson& operator =(const cPoisson& other);
860  virtual std::string str() const override;
862 
865  void setLambda(double lambda) {this->lambda = lambda;}
866  double getLambda() const {return lambda;}
868 
871  virtual double draw() const override;
873 };
874 
875 } // namespace omnetpp
876 
877 #endif
878 
Generates random numbers from the Poisson distribution.
Definition: crandom.h:846
Generates random numbers from the exponential distribution.
Definition: crandom.h:115
Generates random numbers from the Weibull distribution.
Definition: crandom.h:519
Generates random numbers from the chi-square distribution.
Definition: crandom.h:358
virtual cChiSquare * dup() const override
Definition: crandom.h:370
virtual cParetoShifted * dup() const override
Definition: crandom.h:574
virtual cBeta * dup() const override
Definition: crandom.h:289
Generates random numbers from the binomial distribution.
Definition: crandom.h:726
virtual cWeibull * dup() const override
Definition: crandom.h:532
Generates random numbers from the uniform distribution.
Definition: crandom.h:75
virtual cCauchy * dup() const override
Definition: crandom.h:447
Abstract interface for random number generator classes.
Definition: crng.h:49
virtual cGeometric * dup() const override
Definition: crandom.h:779
virtual cIntUniformExcl * dup() const override
Definition: crandom.h:660
Generates random numbers from the normal distribution.
Definition: crandom.h:153
virtual cPoisson * dup() const override
Definition: crandom.h:858
Generates random numbers from the truncated normal distribution.
Definition: crandom.h:194
virtual cBernoulli * dup() const override
Definition: crandom.h:700
virtual cGamma * dup() const override
Definition: crandom.h:248
Generates random numbers from the geometric distribution.
Definition: crandom.h:767
virtual cUniform * dup() const override
Definition: crandom.h:88
virtual cStudentT * dup() const override
Definition: crandom.h:408
virtual cNegBinomial * dup() const override
Definition: crandom.h:818
virtual cIntUniform * dup() const override
Definition: crandom.h:619
Generates random numbers from the shifted Pareto distribution.
Definition: crandom.h:560
A cObject that keeps track of its owner. It serves as base class for many classes in the OMNeT++ libr...
Definition: cownedobject.h:104
Generates random numbers from the beta distribution.
Definition: crandom.h:276
virtual cBinomial * dup() const override
Definition: crandom.h:739
virtual cTruncNormal * dup() const override
Definition: crandom.h:207
virtual void setRNG(cRNG *rng)
Definition: crandom.h:49
Generates random numbers from the Erlang distribution.
Definition: crandom.h:317
Generates random numbers from the gamma distribution.
Definition: crandom.h:235
Generates random numbers from the negative binomial distribution.
Definition: crandom.h:805
virtual cNormal * dup() const override
Definition: crandom.h:166
Definition: cabstracthistogram.h:21
Abstract interface for random variate generator classes.
Definition: crandom.h:31
Generates random numbers from the Cauchy distribution.
Definition: crandom.h:434
Generates random numbers from Student&#39;s T distribution.
Definition: crandom.h:396
Generates random numbers from the triangular distribution.
Definition: crandom.h:475
virtual cExponential * dup() const override
Definition: crandom.h:127
virtual cTriang * dup() const override
Definition: crandom.h:489
Generates random numbers that are the results of Bernoulli trials.
Definition: crandom.h:688
virtual cErlang * dup() const override
Definition: crandom.h:330
Generates random numbers from the discrete uniform distribution.
Definition: crandom.h:606
cRNG * getRNG() const
Definition: crandom.h:54
Generates random numbers from the discrete uniform distribution.
Definition: crandom.h:647