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