MT19937 Struct Reference
[General utility functions.]

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 32 of file mt19937.h.


Constructor & Destructor Documentation

MT19937::MT19937 (  )  [inline]

Definition at line 42 of file mt19937.h.

00043         :rng(0)
00044         {}

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 48 of file mt19937.h.

References rng.

Referenced by fit_spots_historic().

00049         {
00050             rng.RandomInit(seed);
00051         }

void MT19937::copy_state ( const MT19937 r  )  [inline]

Duplicate RNG state.

Parameters:
r RNG to duplicate

Definition at line 55 of file mt19937.h.

References rng.

00056         {
00057             rng = r.rng;
00058         }

double MT19937::operator() (  )  [inline]

Generate a double.

Definition at line 61 of file mt19937.h.

References rng.

00062         {
00063             return rng.Random();
00064         }

uint32_t MT19937::rand_int (  )  [inline]

Generate an int.

Definition at line 67 of file mt19937.h.

References rng.

00068         {
00069             return rng.BRandom();
00070         }

double MT19937::gaussian (  )  [inline]

Generate a Gaussian variate.

Definition at line 73 of file mt19937.h.

00074         {
00075             double x1, x2, w;
00076             do {
00077                 x1 = 2.0 * (*this)() - 1.0;
00078                 x2 = 2.0 * (*this)() - 1.0;
00079                 w = x1 * x1 + x2 * x2;
00080             } while ( w >= 1.0 );
00081 
00082             w = std::sqrt( (-2.0 * std::log( w ) ) / w );
00083             return x1 * w;
00084             //spare so we don't have to save that one extra bit of state y2 = x2 * w;
00085         }

void MT19937::write ( std::ostream &  o  )  [inline]

Serialise state.

Parameters:
o Stream to serialise to

Definition at line 89 of file mt19937.h.

References rng.

Referenced by fit_spots_historic(), and FitSpots::optimize_each_spot_in_turn_for_several_passes().

00090         {
00091             using namespace std;
00092             char f = o.fill();
00093             ios_base::fmtflags fl = o.flags();
00094             o << "MT19937 " << hex << setfill('0') << setw(3) << rng.get_index();   
00095             for(int i=0; i < MERS_N; i++)
00096                 o << " " << hex << setw(8) << rng.get_state()[i];
00097             
00098             o << setfill(f) << setiosflags(fl);
00099         }

void MT19937::read ( std::istream &  is  )  [inline]

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

Definition at line 103 of file mt19937.h.

References rng.

Referenced by fit_spots_historic().

00104         {
00105             using namespace std;
00106 
00107             string ls;
00108             getline(is, ls);
00109             if(ls.size() != 5627)
00110             {
00111                 cerr << "MT19937: Expected string of length 5627. Got " << ls.size() << endl;
00112                 throw ParseError();
00113             }
00114 
00115             istringstream l(ls);
00116             
00117             string s;
00118             uint32_t i;
00119 
00120             l >> s;
00121             
00122             if(s != "MT19937")
00123             {   
00124                 cerr << "MT19937: Expected MT19937. Got " << s << endl;
00125                 throw ParseError();
00126             }       
00127 
00128             for(int n=0; n < MERS_N + 1; n++)
00129             {
00130                 l >> hex >> i;
00131                 if(l.bad())
00132                 {
00133                     cerr << "MT19937: Expected hex number. Got ";
00134                     if(l.eof())
00135                         cerr << "EOF" << endl;
00136                     else
00137                     {
00138                         cerr << l.get() << endl;
00139                     }
00140 
00141                     throw ParseError();
00142                 }
00143 
00144                 if(n==0)
00145                     rng.get_index() = i;
00146                 else
00147                     rng.get_state()[n-1]=i;
00148 
00149             }
00150 
00151         }


Member Data Documentation

CRandomMersenne MT19937::rng

Underlying RNG.

Definition at line 39 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:
Generated on Wed Nov 2 18:00:03 2011 for BCUBED by  doxygen 1.6.3