Use random walk to get continuous random value


/ Published in: C++
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.