LocARNA-2.0.0
discrete_distribution.hh
1 #ifndef LOCARNA_DISCRETE_DISTRIBUTION
2 #define LOCARNA_DISCRETE_DISTRIBUTION
3 
4 #ifdef HAVE_CONFIG_H
5 #include <config.h>
6 #endif
7 
8 namespace LocARNA {
9 
10  // ------------------------------------------------------------
14  private:
15  std::vector<double> distrib_acc_;
16 
17  public:
23  explicit
24  DiscreteDistribution(const std::vector<double> &distvec) {
25  if (distvec.size() == 0)
26  return;
27 
28  distrib_acc_.resize(distvec.size());
29 
30  distrib_acc_[0] = distvec[0];
31  for (size_t i = 1; i < distvec.size(); i++) {
32  distrib_acc_[i] += distrib_acc_[i - 1] + distvec[i];
33  }
34  double max_acc = distrib_acc_[distvec.size() - 1];
35  for (size_t i = 0; i < distvec.size(); i++) {
36  distrib_acc_[i] /= max_acc;
37  }
38  }
39 
46  size_t
47  operator()(size_t x) {
48  double y = x / (double)RAND_MAX;
49 
50  size_t i = 0;
51  for (; i < distrib_acc_.size() && y > distrib_acc_[i]; ++i)
52  ;
53 
54  assert(i <= distrib_acc_.size());
55  return i;
56  }
57  };
58 
59 } // end namespace LocARNA
60 
61 #endif // LOCARNA_DISCRETE_DISTRIBUTION
Definition: discrete_distribution.hh:13
size_t operator()(size_t x)
get random number
Definition: discrete_distribution.hh:47
DiscreteDistribution(const std::vector< double > &distvec)
Construct with distribution vector.
Definition: discrete_distribution.hh:24
Definition: aligner.cc:15