MADNESS 0.10.1
madness::Random Class Reference

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

#include <ran.h>

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 Attributes

const double beta
int cur
std::mutex mutex_
 serializes access to the stream; mutable so getstate() can be const
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:77
double *const u
Definition ran.h:79
const int s
Definition ran.h:76
const int r
Definition ran.h:75
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 beta, cur, r, s, setstate(), and u.

Referenced by test().

◆ ~Random()

madness::Random::~Random ( )
virtual

References u.

Member Function Documentation

◆ generate()

void madness::Random::generate ( )
private

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

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

◆ get()

double madness::Random::get ( )
inline

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

◆ getbytes()

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

Returns vector of random bytes in [0,256).

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

◆ getstate()

RandomState madness::Random::getstate ( ) const

Returns full state of the generator.

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

◆ getv()

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

Returns a vector of uniform doubles in [0,1).

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

◆ setstate() [1/2]

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

Restores state of the generator.

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

Referenced by Random().

◆ setstate() [2/2]

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

Sets state of the generator from integer.

References generate(), lo, mutex_, 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 Random(), b, madness::f, and r.

Member Data Documentation

◆ beta

const double madness::Random::beta
private

Referenced by Random(), and generate().

◆ cur

int madness::Random::cur
private

◆ mutex_

std::mutex madness::Random::mutex_
mutableprivate

serializes access to the stream; mutable so getstate() can be const

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

◆ r

const int madness::Random::r
private

◆ s

const int madness::Random::s
private

Referenced by Random(), 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: