MADNESS 0.10.1
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | 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 ()
 
void getbytes (int n, unsigned char *MADNESS_RESTRICT v)
 Returns vector of random bytes in [0,256)
 
RandomState getstate () const
 Returns full state of the generator.
 
template<typename T >
void getv (int n, T *MADNESS_RESTRICT v)
 Returns a vector of uniform doubles in [0,1)
 
void setstate (const RandomState &s)
 Restores state of the generator.
 
void setstate (unsigned int seed)
 Sets state of the generator from integer.
 

Static Public Member Functions

static void test ()
 Test the generator.
 

Private Member Functions

void generate ()
 
unsigned int simple ()
 
- Private Member Functions inherited from madness::Mutex
 Mutex (int junk=0)
 Make and initialize a mutex ... initial state is unlocked.
 
virtual ~Mutex ()
 
void lock () const
 Acquire the mutex waiting if necessary.
 
pthread_mutex_t * ptr () const
 Return a pointer to the pthread mutex for use by a condition variable.
 
bool try_lock () const
 Try to acquire the mutex ... return true on success, false on failure.
 
void unlock () const
 Free a mutex owned by this thread.
 

Private Attributes

const double beta
 
int cur
 
const int r
 
const int s
 
unsigned int simple_state
 
double *const u
 

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
const double beta
Definition ran.h:75
double *const u
Definition ran.h:77
const int s
Definition ran.h:74
const int r
Definition ran.h:73
static const double m
Definition relops.cc:9
static const double alpha
Definition testcosine.cc:10

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

◆ Random()

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

References setstate().

◆ ~Random()

madness::Random::~Random ( )
virtual

References u.

Member Function Documentation

◆ generate()

void madness::Random::generate ( )
private

References beta, cur, r, s, and u.

Referenced by get(), getbytes(), getv(), and setstate().

◆ get()

double madness::Random::get ( )
inline

◆ getbytes()

void madness::Random::getbytes ( int  n,
unsigned char *MADNESS_RESTRICT  v 
)

Returns vector of random bytes in [0,256)

References cur, generate(), r, u, and v.

◆ getstate()

RandomState madness::Random::getstate ( ) const

Returns full state of the generator.

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

◆ getv()

template<typename T >
void madness::Random::getv ( int  n,
T *MADNESS_RESTRICT  v 
)
inline

◆ setstate() [1/2]

void madness::Random::setstate ( const RandomState s)

◆ setstate() [2/2]

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

Sets state of the generator from integer.

References generate(), lo, r, simple(), simple_state, and u.

◆ simple()

unsigned int madness::Random::simple ( )
private

References simple_state.

Referenced by setstate().

◆ test()

void madness::Random::test ( )
static

Test the generator.

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

Member Data Documentation

◆ beta

const double madness::Random::beta
private

Referenced by generate().

◆ cur

int madness::Random::cur
private

◆ r

const int madness::Random::r
private

◆ s

const int madness::Random::s
private

Referenced by generate(), getstate(), and setstate().

◆ simple_state

unsigned int madness::Random::simple_state
private

Referenced by setstate(), and simple().

◆ u

double* const madness::Random::u
private

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