Infinite precision random numbers. More...
#include <RandomLib-2010-01/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 |
Related Functions | |
(Note that these are not member functions.) | |
template<int bits> | |
std::ostream & | operator<< (std::ostream &os, const RandomNumber< bits > &n) |
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.)
RandomLib::RandomNumber::RandomNumber | ( | int | n = 0 | ) | [inline] |
Constructor sets number to n + (0,1).
Definition at line 49 of file RandomNumber.hpp.
int RandomLib::RandomNumber::Integer | ( | ) | const [inline] |
unsigned RandomLib::RandomNumber::Digit | ( | Random & | r, |
unsigned | k | ||
) | [inline] |
Return digit number k, generating it if necesary.
Definition at line 57 of file RandomNumber.hpp.
References ExpandTo(), and _f.
Referenced by LessThan(), Fraction(), and Value().
unsigned RandomLib::RandomNumber::RawDigit | ( | unsigned | k | ) | const [inline] |
Return digit number k, without generating new digits.
Definition at line 64 of file RandomNumber.hpp.
References _f.
Referenced by Fraction(), Value(), and Range().
void RandomLib::RandomNumber::AddInteger | ( | int | k | ) | [inline] |
void RandomLib::RandomNumber::SetInteger | ( | int | k | ) | [inline] |
Set integer part of RandomNumber k
Definition at line 74 of file RandomNumber.hpp.
References _n.
Referenced by RandomLib::ExactExponential::operator()().
void RandomLib::RandomNumber::Init | ( | int | n = 0 | ) | [inline] |
Return to initial state, uniformly distributed in n + (0,1).
Definition at line 78 of file RandomNumber.hpp.
References STATIC_ASSERT, w, _n, and _f.
Referenced by RandomLib::ExactExponential::operator()(), RandomLib::ExactExponential::ExpFraction(), and RandomLib::ExactPower::operator()().
bool RandomLib::RandomNumber::LessThan | ( | Random & | r, |
RandomNumber & | t | ||
) | [inline] |
Compare two RandomNumbers, *this < t
Definition at line 87 of file RandomNumber.hpp.
Referenced by RandomLib::ExactExponential::ExpFraction(), and RandomLib::ExactPower::operator()().
void RandomLib::RandomNumber::Negate | ( | ) | [inline] |
Change sign of a RandomNumber
Definition at line 106 of file RandomNumber.hpp.
References w, Size(), _f, and _n.
Referenced by Value().
size_t RandomLib::RandomNumber::Size | ( | ) | const [inline] |
Return the number of digits in fraction
Definition at line 116 of file RandomNumber.hpp.
References _f.
RealType RandomLib::RandomNumber::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 126 of file RandomNumber.hpp.
References STATIC_ASSERT, Digit(), and RawDigit().
RealType RandomLib::RandomNumber::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 150 of file RandomNumber.hpp.
References STATIC_ASSERT, Integer(), Negate(), highest_bit_idx(), Digit(), and RawDigit().
std::pair<double, double> RandomLib::RandomNumber::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 206 of file RandomNumber.hpp.
References Integer(), Size(), and RawDigit().
void RandomLib::RandomNumber::ExpandTo | ( | Random & | r, |
size_t | k | ||
) | [inline, private] |
Fill RandomNumber to k digits.
Definition at line 229 of file RandomNumber.hpp.
References _f.
Referenced by Digit().
static int RandomLib::RandomNumber::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 241 of file RandomNumber.hpp.
Referenced by Value().
std::ostream & operator<< | ( | std::ostream & | os, |
const RandomNumber< bits > & | n | ||
) | [related] |
Print a RandomNumber. Format is n.dddd... where the base for printing is 2max(4,b). The ... represents an infinite sequence of ungenerated random digits (uniformly distributed). Thus with b = 1, 0.0... = (0,1/2), 0.00... = (0,1/4), 0.11... = (3/4,1), etc.
Definition at line 265 of file RandomNumber.hpp.
int RandomLib::RandomNumber::_n [private] |
The integer part
Definition at line 221 of file RandomNumber.hpp.
Referenced by Integer(), AddInteger(), SetInteger(), Init(), LessThan(), and Negate().
std::vector<unsigned> RandomLib::RandomNumber::_f [private] |
The fraction part
Definition at line 225 of file RandomNumber.hpp.
Referenced by Digit(), RawDigit(), Init(), Negate(), Size(), and ExpandTo().
const int RandomLib::RandomNumber::w = std::numeric_limits<unsigned>::digits [static, private] |
The number of bits in unsigned.
Definition at line 254 of file RandomNumber.hpp.