Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Boost.Random example 2: storing and retrieving a normal distribution's state

 

Boost.Random example 2: storing and retrieving a normal distribution's state is a Boost.Random example.

 

 

 

 

 

 

 

#include <cassert>
#include <fstream>
#include <iostream>
#include <boost/random/normal_distribution.hpp>
#include <boost/random/lagged_fibonacci.hpp>
//From http://www.richelbilderbeek.nl/CppRandomExample2.htm
struct MyRandomNormal
{
  MyRandomNormal()
  {
    {
      std::ifstream f("engine.txt");
      f >> m_engine;
    }
    {
      std::ifstream f("norm_dist.txt");
      f >> m_norm_dist;
    }
  }
  MyRandomNormal(const double mean, const double stddev)
    : m_norm_dist(mean,stddev)
  {
  }
  ~MyRandomNormal()
  {
    {
      std::ofstream f("engine.txt");
      f << m_engine;
    }
    {
      std::ofstream f("norm_dist.txt");
      f << m_norm_dist;
    }
  }
  double Get()
  {
    return m_norm_dist.operator ()<boost::lagged_fibonacci19937>((m_engine));
  }
  boost::lagged_fibonacci19937 m_engine;
  boost::normal_distribution<double> m_norm_dist;
};
int main()
{
  const double mean = 1.1;
  const double stddev = 2.2;
  {
    MyRandomNormal x(mean,stddev);
  }
  {
    MyRandomNormal x;
    assert(x.m_norm_dist.mean() == mean);
    assert(x.m_norm_dist.sigma() == stddev);
  }
}


 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml