Skip to content
Snippets Groups Projects
MersenneTwister.cpp 1.81 KiB
Newer Older
//------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------
#include "MantidKernel/MersenneTwister.h"

namespace Mantid 
{
  namespace Kernel 
  {

    //------------------------------------------------------------------------------
    // Public member functions
    //------------------------------------------------------------------------------
    
    /**
     * Default constructor. Sets the range to [0.0,1.0]
     */
    MersenneTwister::MersenneTwister() : 
      m_generator(), m_uniform_dist(0.0, 1.0)
    {
    }

    /**
     * (Re-)seed the generator
     * @param seedValue :: A seed for the generator
    void MersenneTwister::setSeed(unsigned int seedValue)
      // Bug in earlier versions of this implementation meant
      // that a unsigned int could not be past to the seed function
      m_generator.seed((boost::mt19937::result_type)seedValue);
    }

    /**
     * Sets the range of the subsequent calls to next() 
     * @param start :: The lowest value a call to next() will produce
     * @param end :: The largest value a call to next() will produce
     */
    void MersenneTwister::setRange(double start, double end)
    {
      m_uniform_dist = uniform_double(start,end);
    }
    
    /**
     * Returns the next number in the pseudo-random sequence generated by
     * the Mersenne Twister 19937 algorithm.
     * @returns The next number in the pseudo-random sequence
     */
    double MersenneTwister::next()
    {
      /// A variate generator to combine a random number generator with a distribution
      uniform_generator uniform_rand(m_generator, m_uniform_dist);      
      // There is no reason why this call shouldn't be considered const
      return uniform_rand();
    }

  }
}