X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=sampling.h;h=8f80b72eca8d4982731d15a366100b5c68563ed3;hb=5d607690061273a9fca1262be1473038dc9fb3da;hp=417140395b8b78801af1808fc93bf6d3eca9d602;hpb=946f9a6adb2a82048c8453d44693cd3838d32939;p=rsem.git diff --git a/sampling.h b/sampling.h index 4171403..8f80b72 100644 --- a/sampling.h +++ b/sampling.h @@ -5,17 +5,48 @@ #include #include #include +#include #include "boost/random.hpp" -boost::mt19937 rng(time(NULL)); -boost::uniform_01 rg(rng); +typedef unsigned int seedType; +typedef boost::mt19937 engine_type; +typedef boost::gamma_distribution<> gamma_dist; +typedef boost::uniform_01 uniform01; +typedef boost::variate_generator gamma_generator; + +class engineFactory { +public: + static void init() { seedEngine = new engine_type(time(NULL)); } + static void init(seedType seed) { seedEngine = new engine_type(seed); } + + static void finish() { if (seedEngine != NULL) delete seedEngine; } + + static engine_type *new_engine() { + seedType seed; + static std::set seedSet; // empty set of seeds + std::set::iterator iter; + + do { + seed = (*seedEngine)(); + iter = seedSet.find(seed); + } while (iter != seedSet.end()); + seedSet.insert(seed); + + return new engine_type(seed); + } + + private: + static engine_type *seedEngine; +}; + +engine_type* engineFactory::seedEngine = NULL; // arr should be cumulative! // interval : [,) // random number should be in [0, arr[len - 1]) // If by chance arr[len - 1] == 0.0, one possibility is to sample uniformly from 0...len-1 -int sample(std::vector& arr, int len) { +int sample(uniform01& rg, std::vector& arr, int len) { int l, r, mid; double prb = rg() * arr[len - 1];