We Recommend

C++ The Core Language C++ The Core Language
C++: The Core Language is for C programmers transitioning to C++. It's designed to get readers up to speed quickly by covering an essential subset of the language. The subset consists of features without which it's just not C++, and a handful of others that make it a reasonably useful language.


Posted By

tkf on 07/20/08


Tagged


Versions (?)


Use random walk to get continuous random value


Published in: C++ 


  1. class ContinuousRand {
  2. public:
  3. double alpha, x;
  4. boost::mt19937 gen;
  5. boost::uniform_smallint<> dst;
  6. boost::variate_generator<
  7. boost::mt19937,boost::uniform_smallint<> > rand;
  8. ContinuousRand(double _alpha): alpha(_alpha), x(0),
  9. gen( static_cast<unsigned long>(std::time(0)) ), dst(0,1),
  10. rand( gen, dst )
  11. {}
  12. double operator()(){
  13. x += (static_cast<double>(rand())-0.5)*2.0;
  14. x *= 1.0 - 1.0/alpha;
  15. return x/sqrt(alpha);
  16. }
  17. };

Report this snippet 

You need to login to post a comment.