]> git.donarmstrong.com Git - rsem.git/blob - simul.h
Deleted a ';' at the end of RSEM v1.2.15 updates
[rsem.git] / simul.h
1 #ifndef SIMUL_H_
2 #define SIMUL_H_
3
4 #include<cassert>
5
6 #include "boost/random.hpp"
7
8 class simul {
9 public:
10
11  simul(unsigned int seed) : engine(seed), rg(engine, boost::random::uniform_01<>()) {
12   }
13
14         // interval : [,)
15         // random number should be in [0, arr[len - 1])
16         // If by chance arr[len - 1] == 0.0, one possibility is to sample uniformly from 0 ... len - 1
17         int sample(double* arr, int len) {
18           int l, r, mid;
19           double prb = random() * arr[len - 1];
20
21
22           l = 0; r = len - 1;
23           while (l <= r) {
24             mid = (l + r) / 2;
25             if (arr[mid] <= prb) l = mid + 1;
26             else r = mid - 1;
27           }
28
29           if (l >= len) { printf("%d %lf %lf\n", len, arr[len - 1], prb); }
30           assert(l < len);
31
32           return l;
33         }
34
35         double random() { return rg(); };
36
37 private:
38         boost::random::mt19937 engine;
39         boost::random::variate_generator<boost::random::mt19937&, boost::random::uniform_01<> > rg;
40 };
41
42 #endif /* SIMUL_H_ */
43