MTMCSim  0.5
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
discrete_distribution.h
Go to the documentation of this file.
1 #ifndef _DISCRETE_DISTRIBUTION_H
2 #define _DISCRETE_DISTRIBUTION_H
3 #include "StdHeaders.h"
4 
5 
6 namespace MTMCSim{
7 
14 public:
16  {}
17 
22  discrete_distribution(unsigned int n_, const vector<double>& prob_);
23 
29  discrete_distribution(unsigned int n_, const vector<double>& prob_, const vector<double>& val_);
30 
31  void setDistribution(unsigned int n_, const vector<double>& prob_);
32 
33  void setDistributionWOGenTab(unsigned int n_, const vector<double>& prob_);
34 
39  template<typename Engine> double operator()(Engine & engine) const;
40 
41  double getMean() const
42  { return mean;}
43 
46  double getStdev() const
47  { return stdev;}
48 
51  // To generate one RV with desired distribution,
52  // two uniformly distributed RV is needed
53  int consumeUniRV() const
54  { return 2; }
55 
56  void loadTab(string& tabStr);
57 
58  void tabToStr(string& tabStr);
59 
60 private:
61  void genTab();
62  unsigned int n;
63  vector<double> prob;
64  vector<double> val;
65  double mean, stdev;
66  vector<double> p; // for internal algorithm use
67  vector<int> ialt; // for internal algorithm use
68 
69  bool hasValidTable;
70 };
71 
72 
73 template<typename Engine> double discrete_distribution::operator()(Engine & uniEng) const
74 {
75  if (!hasValidTable)
76  throw runtime_error("Has not set the table yet!!");
77  int i = (int)(n*uniEng());
78  if (uniEng() < p[i])
79  i = ialt[i];
80  return val.empty() ? i : val[i];
81 }
82 
83 
84 }
85 
86 #endif