MADNESS 0.10.1
|
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 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 |
A random number generator (portable, vectorized, and thread-safe)
Following Brent 1992, we use a 48-bit generalized Fibonacci generator
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.
madness::Random::Random | ( | unsigned int | seed = 5461 | ) |
References setstate().
|
virtual |
References u.
|
private |
References beta, cur, r, s, and u.
Referenced by get(), getbytes(), getv(), and setstate().
|
inline |
References cur, generate(), r, and u.
Referenced by madness::RandomValue< double >(), and madness::RandomValue< float >().
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.
RandomState madness::Random::getstate | ( | ) | const |
Returns full state of the generator.
References madness::RandomState::cur, cur, r, s, and u.
Returns a vector of uniform doubles in [0,1)
References cur, generate(), r, T(), u, and v.
Referenced by madness::RandomVector< double >(), madness::RandomVector< double_complex >(), madness::RandomVector< float >(), madness::RandomVector< float_complex >(), and test().
void madness::Random::setstate | ( | const RandomState & | s | ) |
Restores state of the generator.
Referenced by Random(), madness::Localizer::localize_boys(), madness::Localizer::localize_new(), main(), main(), test(), test_Kcommutator(), test_lowrank_function(), and test_math().
void madness::Random::setstate | ( | unsigned int | seed | ) |
Sets state of the generator from integer.
References generate(), lo, r, simple(), simple_state, and u.
|
private |
References simple_state.
Referenced by setstate().
|
static |
Test the generator.
References b, madness::f, getv(), and r.
|
private |
Referenced by generate().
|
private |
Referenced by generate(), get(), getbytes(), getstate(), getv(), and setstate().
|
private |
Referenced by generate(), get(), getbytes(), getstate(), getv(), setstate(), setstate(), and test().
|
private |
Referenced by generate(), getstate(), and setstate().
|
private |
Referenced by setstate(), and simple().
|
private |
Referenced by ~Random(), generate(), get(), getbytes(), getstate(), getv(), setstate(), and setstate().