MADNESS  version 0.9
Public Member Functions | Static Public Member Functions | List of all members
madness::Random Class Reference

A random number generator (portable, vectorized, and thread-safe) More...

#include <ran.h>

Inheritance diagram for madness::Random:
Inheritance graph
[legend]
Collaboration diagram for madness::Random:
Collaboration graph
[legend]

Public Member Functions

 Random (unsigned int seed=5461)
 
virtual ~Random ()
 
double get ()
 
template<typename T >
void getv (int n, T *restrict v)
 Returns a vector of uniform doubles in [0,1) More...
 
void getbytes (int n, unsigned char *restrict v)
 Returns vector of random bytes in [0,256) More...
 
RandomState getstate () const
 Returns full state of the generator. More...
 
void setstate (const RandomState &s)
 Restores state of the generator. More...
 
void setstate (unsigned int seed)
 Sets state of the generator from integer. More...
 

Static Public Member Functions

static void test ()
 Test the generator. More...
 

Detailed Description

A random number generator (portable, vectorized, and thread-safe)

Following Brent 1992, we use a 48-bit generalized Fibonacci generator

u[n] = alpha*u[n-r] + beta*u[n-s] mod m

with alpha=1, beta=7, r=1279, s=861, m=2^48. Double precision numbers are used to perform exact integer arithmetic. 48-bit because we have 52 bits of mantissa, alpha+1 is 3 bits and 1 bit spare.

The period is nominally 2^m (2^r - 1) / 2 but if p is the period, X[n] and X[n+p/2k] differ by at most k bits (0 < k < 48) so usage should be limited to the first 2^r-1 entries (about 10^385 values).

Each instance provides a separate stream, but it is up to the user to partition the sequence by selecting distinct seeds or other means.

The streams are thread safe.

A default stream is provided as madness::default_random_generator.

Constructor & Destructor Documentation

madness::Random::Random ( unsigned int  seed = 5461)

References setstate().

madness::Random::~Random ( )
virtual

Member Function Documentation

double madness::Random::get ( )
inline
void madness::Random::getbytes ( int  n,
unsigned char *restrict  v 
)

Returns vector of random bytes in [0,256)

References mpfr::min().

Referenced by test().

RandomState madness::Random::getstate ( ) const

Returns full state of the generator.

References madness::RandomState::cur, and madness::RandomState::u.

template<typename T >
void madness::Random::getv ( int  n,
T *restrict  v 
)
inline
void madness::Random::setstate ( const RandomState s)

Restores state of the generator.

References madness::RandomState::cur, and madness::RandomState::u.

Referenced by Random(), and test().

void madness::Random::setstate ( unsigned int  seed)

Sets state of the generator from integer.

void madness::Random::test ( )
static

Test the generator.

References b(), madness::f, getbytes(), and getv().


The documentation for this class was generated from the following files: