RNG.h
Go to the documentation of this file.
00001 #ifndef BWI_TOOLS_RNG_H
00002 #define BWI_TOOLS_RNG_H
00003 
00004 #include <boost/random/uniform_real.hpp>
00005 #include <boost/random/uniform_int.hpp>
00006 #include <boost/random/poisson_distribution.hpp>
00007 #include <boost/random/variate_generator.hpp>
00008 #include <boost/random/mersenne_twister.hpp>
00009 
00010 #include <cstdlib>
00011 #include <vector>
00012 
00013 class RNG : public boost::mt19937 {
00014 
00015   public:
00016     RNG (unsigned int seed) : boost::mt19937(seed) {}
00017 
00018     inline float randomFloat() {
00019       boost::uniform_real<float> dist;
00020       boost::variate_generator<boost::mt19937&, boost::uniform_real<float> > gen(*this, dist);
00021       return gen();
00022     }
00023 
00024     inline int randomInt(int min, int max) {
00025       boost::uniform_int<int> dist(min, max);
00026       boost::variate_generator<boost::mt19937&, boost::uniform_int<int> > gen(*this, dist);
00027       return gen();
00028     }
00029 
00030     inline int randomInt(int max = std::numeric_limits<int>::max()) {
00031       return randomInt(0, max);
00032     }
00033 
00034     inline int randomUInt(unsigned int max = std::numeric_limits<unsigned int>::max()) {
00035       boost::uniform_int<unsigned int> dist(0, max);
00036       boost::variate_generator<boost::mt19937&, boost::uniform_int<unsigned int> > gen(*this, dist);
00037       return gen();
00038     }
00039 
00040     inline int poissonInt(int mean) {
00041       boost::poisson_distribution<int> dist(mean);
00042       boost::variate_generator<boost::mt19937&, boost::poisson_distribution<int> > gen(*this, dist);
00043       return gen();
00044     }
00045 
00046     inline void randomOrdering(std::vector<unsigned int> &inds) {
00047       unsigned int j;
00048       unsigned int temp;
00049       for (unsigned int i = 0; i < inds.size(); i++) {
00050         inds[i] = i;
00051       }
00052       for (int i = (int)inds.size() - 1; i >= 0; i--) {
00053         j = randomInt(i + 1);
00054         temp = inds[i];
00055         inds[i] = inds[j];
00056         inds[j] = temp;
00057       }
00058     }
00059 
00060     inline int select(const std::vector<float>& probabilities) {
00061       float random_value = randomFloat();
00062       float prob_sum = probabilities[0];
00063       for (int i = 1; i < probabilities.size(); ++i) {
00064         if (random_value < prob_sum) return i - 1;
00065         prob_sum += probabilities[i];
00066       }
00067       return probabilities.size() - 1;
00068     }
00069 };
00070 
00071 #endif /* end of include guard: BWI_TOOLS_RNG_H */


bwi_tools
Author(s): Piyush Khandelwal
autogenerated on Thu Jun 6 2019 17:57:26