ThreeB 1.1
|
Useful wrapper for MT19937 random number generator class. More...
#include <mt19937.h>
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 &) |
MT19937::MT19937 | ( | const MT19937 & | ) | [private] |
Disallow copying, since one almost never wants to do this.
Copying has to be explicit via MT19937::copy_state().
void MT19937::simple_seed | ( | int | seed | ) | [inline] |
void MT19937::copy_state | ( | const MT19937 & | r | ) | [inline] |
double MT19937::operator() | ( | ) | [inline] |
uint32_t MT19937::rand_int | ( | ) | [inline] |
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] |
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; } }
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().