ThreeB 1.1
Classes | Public Member Functions | Public Attributes | Private Member Functions
MT19937 Struct Reference

Useful wrapper for MT19937 random number generator class. More...

#include <mt19937.h>

List of all members.

Classes

struct  ParseError
 Null struct thrown if attempting to load state from stream yields a parse error. More...

Public Member Functions

 MT19937 ()
void simple_seed (int seed)
void copy_state (const MT19937 &r)
double operator() ()
uint32_t rand_int ()
double gaussian ()
void write (std::ostream &o)
void read (std::istream &is)

Public Attributes

CRandomMersenne rng

Private Member Functions

 MT19937 (const MT19937 &)

Detailed Description

Useful wrapper for MT19937 random number generator class.

Definition at line 13 of file mt19937.h.


Constructor & Destructor Documentation

MT19937::MT19937 ( ) [inline]

Definition at line 23 of file mt19937.h.

        :rng(0)
        {}
MT19937::MT19937 ( const MT19937 ) [private]

Disallow copying, since one almost never wants to do this.

Copying has to be explicit via MT19937::copy_state().


Member Function Documentation

void MT19937::simple_seed ( int  seed) [inline]

Seed state with a simple RNG.

Parameters:
seed

Definition at line 29 of file mt19937.h.

References rng.

        {
            rng.RandomInit(seed);
        }
void MT19937::copy_state ( const MT19937 r) [inline]

Duplicate RNG state.

Parameters:
rRNG to duplicate

Definition at line 36 of file mt19937.h.

References rng.

        {
            rng = r.rng;
        }
double MT19937::operator() ( ) [inline]

Generate a double.

Definition at line 42 of file mt19937.h.

References rng.

        {
            return rng.Random();
        }
uint32_t MT19937::rand_int ( ) [inline]

Generate an int.

Definition at line 48 of file mt19937.h.

References rng.

        {
            return rng.BRandom();
        }
double MT19937::gaussian ( ) [inline]

Generate a Gaussian variate.

Definition at line 54 of file mt19937.h.

        {
            double x1, x2, w;
            do {
                x1 = 2.0 * (*this)() - 1.0;
                x2 = 2.0 * (*this)() - 1.0;
                w = x1 * x1 + x2 * x2;
            } while ( w >= 1.0 );

            w = std::sqrt( (-2.0 * std::log( w ) ) / w );
            return x1 * w;
            //spare so we don't have to save that one extra bit of state y2 = x2 * w;
        }
void MT19937::write ( std::ostream &  o) [inline]

Serialise state.

Parameters:
oStream to serialise to

Definition at line 70 of file mt19937.h.

References rng.

        {
            using namespace std;
            char f = o.fill();
            ios_base::fmtflags fl = o.flags();
            o << "MT19937 " << hex << setfill('0') << setw(3) << rng.get_index();   
            for(int i=0; i < MERS_N; i++)
                o << " " << hex << setw(8) << rng.get_state()[i];
            
            o << setfill(f) << setiosflags(fl);
        }
void MT19937::read ( std::istream &  is) [inline]

De serialise state param is Stream to de-serialise from.

Definition at line 84 of file mt19937.h.

References rng.

        {
            using namespace std;

            string ls;
            getline(is, ls);
            if(ls.size() != 5627)
            {
                cerr << "MT19937: Expected string of length 5627. Got " << ls.size() << endl;
                throw ParseError();
            }

            istringstream l(ls);
            
            string s;
            uint32_t i;

            l >> s;
            
            if(s != "MT19937")
            {   
                cerr << "MT19937: Expected MT19937. Got " << s << endl;
                throw ParseError();
            }       

            for(int n=0; n < MERS_N + 1; n++)
            {
                l >> hex >> i;
                if(l.bad())
                {
                    cerr << "MT19937: Expected hex number. Got ";
                    if(l.eof())
                        cerr << "EOF" << endl;
                    else
                    {
                        cerr << l.get() << endl;
                    }

                    throw ParseError();
                }

                if(n==0)
                    rng.get_index() = i;
                else
                    rng.get_state()[n-1]=i;

            }

        }

Member Data Documentation

CRandomMersenne MT19937::rng

Underlying RNG.

Definition at line 20 of file mt19937.h.

Referenced by copy_state(), operator()(), rand_int(), read(), simple_seed(), and write().


The documentation for this struct was generated from the following file: