10 #include "boost/random.hpp"
12 typedef unsigned int seedType;
13 typedef boost::mt19937 engine_type;
14 typedef boost::gamma_distribution<> gamma_dist;
15 typedef boost::uniform_01<engine_type> uniform01;
16 typedef boost::variate_generator<engine_type&, gamma_dist> gamma_generator;
20 static engine_type *new_engine() {
22 static engine_type seedEngine(time(NULL));
23 static std::set<seedType> seedSet; // empty set of seeds
24 std::set<seedType>::iterator iter;
28 iter = seedSet.find(seed);
29 } while (iter != seedSet.end());
32 return new engine_type(seed);
36 // arr should be cumulative!
38 // random number should be in [0, arr[len - 1])
39 // If by chance arr[len - 1] == 0.0, one possibility is to sample uniformly from 0...len-1
40 int sample(uniform01& rg, std::vector<double>& arr, int len) {
42 double prb = rg() * arr[len - 1];
47 if (arr[mid] <= prb) l = mid + 1;
51 if (l >= len) { printf("%d %lf %lf\n", len, arr[len - 1], prb); }