Infinite precision random numbers. More...
#include <RandomLib/RandomNumber.hpp>
Public Member Functions | |
RandomNumber (int n=0) | |
int | Integer () const |
template<class Random > | |
unsigned | Digit (Random &r, unsigned k) |
unsigned | RawDigit (unsigned k) const |
void | AddInteger (int k) |
void | SetInteger (int k) |
void | Init (int n=0) |
template<class Random > | |
bool | LessThan (Random &r, RandomNumber &t) |
void | Negate () |
size_t | Size () const |
template<typename RealType , typename Random > | |
RealType | Fraction (Random &r) |
template<typename RealType , class Random > | |
RealType | Value (Random &r) |
std::pair< double, double > | Range () const |
Private Member Functions | |
template<class Random > | |
void | ExpandTo (Random &r, size_t k) |
Static Private Member Functions | |
static int | highest_bit_idx (unsigned x) |
Private Attributes | |
int | _n |
std::vector< unsigned > | _f |
Static Private Attributes | |
static const int | w = std::numeric_limits<unsigned>::digits |
Infinite precision random numbers.
Implement infinite precision random numbers. Integer part is non-random. Fraction part consists of any some number of digits in base 2b. If m digits have been generated then the fraction is uniformly distributed in the open interval sumk=1m fk-1/2kb + (0,1)/2mb. When a RandomNumber is first constructed the integer part is zero and m = 0, and the number represents (0,1). A vRandomNumber is able to represent all numbers in the symmetric open interval (-231, 231). In this implementation b must be less than 4 or a multiple of 4. (This restriction allows printing in hexadecimal and can easily be relaxed. There's also no essential reason why the base should be a power of 2.)
Definition at line 45 of file RandomNumber.hpp.
RandomLib::RandomNumber< bits >::RandomNumber | ( | int | n = 0 |
) | [inline] |
Constructor sets number to n + (0,1).
Definition at line 50 of file RandomNumber.hpp.
int RandomLib::RandomNumber< bits >::Integer | ( | ) | const [inline] |
Return integer part of RandomNumber.
Definition at line 54 of file RandomNumber.hpp.
References RandomLib::RandomNumber< bits >::_n.
Referenced by RandomLib::RandomNumber< bits >::Range(), and RandomLib::RandomNumber< bits >::Value().
unsigned RandomLib::RandomNumber< bits >::Digit | ( | Random & | r, | |
unsigned | k | |||
) | [inline] |
Return digit number k, generating it if necesary.
Definition at line 58 of file RandomNumber.hpp.
References RandomLib::RandomNumber< bits >::_f, and RandomLib::RandomNumber< bits >::ExpandTo().
Referenced by RandomLib::RandomNumber< bits >::Fraction(), RandomLib::RandomNumber< bits >::LessThan(), and RandomLib::RandomNumber< bits >::Value().
unsigned RandomLib::RandomNumber< bits >::RawDigit | ( | unsigned | k | ) | const [inline] |
Return digit number k, without generating new digits.
Definition at line 65 of file RandomNumber.hpp.
References RandomLib::RandomNumber< bits >::_f.
Referenced by RandomLib::RandomNumber< bits >::Fraction(), RandomLib::RandomNumber< bits >::Range(), and RandomLib::RandomNumber< bits >::Value().
void RandomLib::RandomNumber< bits >::AddInteger | ( | int | k | ) | [inline] |
Add integer k to RandomNumber
Definition at line 71 of file RandomNumber.hpp.
References RandomLib::RandomNumber< bits >::_n.
void RandomLib::RandomNumber< bits >::SetInteger | ( | int | k | ) | [inline] |
Set integer part of RandomNumber k
Definition at line 75 of file RandomNumber.hpp.
References RandomLib::RandomNumber< bits >::_n.
Referenced by RandomLib::ExactExponential< bits >::operator()().
void RandomLib::RandomNumber< bits >::Init | ( | int | n = 0 |
) | [inline] |
Return to initial state, uniformly distributed in n + (0,1).
Definition at line 79 of file RandomNumber.hpp.
References RandomLib::RandomNumber< bits >::_f, RandomLib::RandomNumber< bits >::_n, STATIC_ASSERT, and RandomLib::RandomNumber< bits >::w.
Referenced by RandomLib::ExactExponential< bits >::ExpFraction(), and RandomLib::ExactExponential< bits >::operator()().
bool RandomLib::RandomNumber< bits >::LessThan | ( | Random & | r, | |
RandomNumber< bits > & | t | |||
) | [inline] |
Compare two RandomNumbers, *this < t
Definition at line 88 of file RandomNumber.hpp.
References RandomLib::RandomNumber< bits >::_n, and RandomLib::RandomNumber< bits >::Digit().
Referenced by RandomLib::ExactExponential< bits >::ExpFraction(), and RandomLib::ExactPower< bits >::operator()().
void RandomLib::RandomNumber< bits >::Negate | ( | ) | [inline] |
Change sign of a RandomNumber
Definition at line 107 of file RandomNumber.hpp.
References RandomLib::RandomNumber< bits >::_f, RandomLib::RandomNumber< bits >::_n, RandomLib::RandomNumber< bits >::Size(), and RandomLib::RandomNumber< bits >::w.
Referenced by RandomLib::RandomNumber< bits >::Value().
size_t RandomLib::RandomNumber< bits >::Size | ( | ) | const [inline] |
Return the number of digits in fraction
Definition at line 117 of file RandomNumber.hpp.
References RandomLib::RandomNumber< bits >::_f.
Referenced by RandomLib::RandomNumber< bits >::Negate(), and RandomLib::RandomNumber< bits >::Range().
RealType RandomLib::RandomNumber< bits >::Fraction | ( | Random & | r | ) | [inline] |
Return the fraction part of the RandomNumber as a floating point number of type RealType rounded to the nearest multiple of 1/2p, where p = std::numeric_limits<RealType>::digits, and, if necessary, creating additional digits of the number.
Definition at line 127 of file RandomNumber.hpp.
References RandomLib::RandomNumber< bits >::Digit(), RandomLib::RandomNumber< bits >::RawDigit(), and STATIC_ASSERT.
RealType RandomLib::RandomNumber< bits >::Value | ( | Random & | r | ) | [inline] |
Return the value of the RandomNumber rounded to nearest floating point number of type RealType and, if necessary, creating additional digits of the number.
Definition at line 151 of file RandomNumber.hpp.
References RandomLib::RandomNumber< bits >::Digit(), RandomLib::RandomNumber< bits >::highest_bit_idx(), RandomLib::RandomNumber< bits >::Integer(), RandomLib::RandomNumber< bits >::Negate(), RandomLib::RandomNumber< bits >::RawDigit(), and STATIC_ASSERT.
std::pair<double, double> RandomLib::RandomNumber< bits >::Range | ( | ) | const [inline] |
Return the range of possible values for the RandomNumber as pair of doubles. This doesn't create any additional digits of the result and doesn't try to control roundoff.
Definition at line 207 of file RandomNumber.hpp.
References RandomLib::RandomNumber< bits >::Integer(), RandomLib::RandomNumber< bits >::RawDigit(), and RandomLib::RandomNumber< bits >::Size().
void RandomLib::RandomNumber< bits >::ExpandTo | ( | Random & | r, | |
size_t | k | |||
) | [inline, private] |
Fill RandomNumber to k digits.
Definition at line 230 of file RandomNumber.hpp.
References RandomLib::RandomNumber< bits >::_f.
Referenced by RandomLib::RandomNumber< bits >::Digit().
static int RandomLib::RandomNumber< bits >::highest_bit_idx | ( | unsigned | x | ) | [inline, static, private] |
Return index [0..32] of highest bit set. Return 0 if x = 0, 32 is if x = ~0. (From Algorithms for programmers by Joerg Arndt.)
Definition at line 242 of file RandomNumber.hpp.
Referenced by RandomLib::RandomNumber< bits >::Value().
int RandomLib::RandomNumber< bits >::_n [private] |
The integer part
Definition at line 222 of file RandomNumber.hpp.
Referenced by RandomLib::RandomNumber< bits >::AddInteger(), RandomLib::RandomNumber< bits >::Init(), RandomLib::RandomNumber< bits >::Integer(), RandomLib::RandomNumber< bits >::LessThan(), RandomLib::RandomNumber< bits >::Negate(), and RandomLib::RandomNumber< bits >::SetInteger().
std::vector<unsigned> RandomLib::RandomNumber< bits >::_f [private] |
The fraction part
Definition at line 226 of file RandomNumber.hpp.
Referenced by RandomLib::RandomNumber< bits >::Digit(), RandomLib::RandomNumber< bits >::ExpandTo(), RandomLib::RandomNumber< bits >::Init(), RandomLib::RandomNumber< bits >::Negate(), RandomLib::RandomNumber< bits >::RawDigit(), and RandomLib::RandomNumber< bits >::Size().
const int RandomLib::RandomNumber< bits >::w = std::numeric_limits<unsigned>::digits [static, private] |
The number of bits in unsigned.
Definition at line 255 of file RandomNumber.hpp.
Referenced by RandomLib::RandomNumber< bits >::Init(), and RandomLib::RandomNumber< bits >::Negate().