RandomLib
1.10
|
Infinite precision random numbers. More...
#include <RandomLib/RandomNumber.hpp>
Public Member Functions | |
RandomNumber () throw () | |
void | swap (RandomNumber &t) throw () |
void | Init () throw () |
int | Sign () const throw () |
void | Negate () throw () |
int | Floor () const throw () |
int | Ceiling () const throw () |
unsigned | UInteger () const throw () |
void | AddInteger (int k) throw () |
template<class Random > | |
bool | LessThan (Random &r, RandomNumber &t) |
template<class Random > | |
bool | GreaterPair (Random &r, RandomNumber &u, RandomNumber &v) |
template<class Random , typename IntType > | |
bool | LessThan (Random &r, IntType p, IntType q) |
template<class Random , typename IntType > | |
bool | LessThan (Random &r, IntType p0, IntType c, IntType q, UniformInteger< IntType, bits > &j) |
template<class Random > | |
unsigned | Digit (Random &r, unsigned k) |
template<class Random > | |
void | AddDigit (Random &r) |
const unsigned & | RawDigit (unsigned k) const throw () |
unsigned & | RawDigit (unsigned k) throw () |
void | Clear () |
unsigned | Size () const throw () |
template<typename RealType , typename Random > | |
RealType | Fraction (Random &r) |
template<typename RealType , class Random > | |
RealType | Value (Random &r) |
std::pair< double, double > | Range () const throw () |
Static Public Member Functions | |
template<class Random > | |
static unsigned | RandomDigit (Random &r) throw () |
Static Public Attributes | |
static const unsigned | mask |
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 ∑k=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 RandomNumber is able to represent all numbers in the symmetric open interval (−231, 231). In this implementation, b must one of 1, 2, 3, 4, 8, 12, 16, 20, 24, 28, or 32. (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.)
bits | the number of bits in each digit. |
Definition at line 41 of file RandomNumber.hpp.
|
inline |
Constructor sets number to a random number uniformly distributed in (0,1).
Definition at line 47 of file RandomNumber.hpp.
|
inline |
Swap with another RandomNumber. This is a fast way of doing an assignment.
[in,out] | t | the RandomNumber to swap with. |
Definition at line 54 of file RandomNumber.hpp.
References std::swap().
|
inline |
Return to initial state, uniformly distributed in (0,1).
Definition at line 64 of file RandomNumber.hpp.
References STATIC_ASSERT.
Referenced by coverage32(), and coverage64().
|
inline |
Definition at line 74 of file RandomNumber.hpp.
Referenced by coverage32(), coverage64(), and RandomLib::RandomNumber< bits >::operator<<().
|
inline |
Change the sign of the RandomNumber.
Definition at line 78 of file RandomNumber.hpp.
Referenced by coverage32(), and coverage64().
|
inline |
Definition at line 82 of file RandomNumber.hpp.
Referenced by RandomLib::RandomNumber< bits >::AddInteger(), coverage32(), and coverage64().
|
inline |
Definition at line 86 of file RandomNumber.hpp.
Referenced by coverage32(), and coverage64().
|
inline |
Definition at line 90 of file RandomNumber.hpp.
Referenced by RandomLib::RandomNumber< bits >::operator<<().
|
inline |
Add integer k to the RandomNumber.
[in] | k | the integer to add. |
Definition at line 96 of file RandomNumber.hpp.
References RandomLib::RandomNumber< bits >::Floor(), RandomLib::RandomNumber< bits >::mask, and RandomLib::RandomNumber< bits >::Size().
Referenced by coverage32(), and coverage64().
|
inline |
Compare with another RandomNumber, *this < t
Random | the type of the random generator. |
[in,out] | r | a random generator. |
[in,out] | t | a RandomNumber to compare. |
Definition at line 112 of file RandomNumber.hpp.
References RandomLib::RandomNumber< bits >::Digit().
Referenced by coverage32(), and coverage64().
|
inline |
Compare RandomNumber with two others, *this > max(u, v)
Random | the type of the random generator. |
[in,out] | r | a random generator. |
[in,out] | u | first RandomNumber to compare. |
[in,out] | v | second RandomNumber to compare. |
Definition at line 133 of file RandomNumber.hpp.
References RandomLib::RandomNumber< bits >::Digit().
|
inline |
Compare with a fraction, *this < p/q
Random | the type of the random generator. |
[in,out] | r | a random generator. |
[in] | p | the numerator of the fraction. |
[in] | q | the denominator of the fraction (require q > 0). |
Definition at line 187 of file RandomNumber.hpp.
References RandomLib::RandomNumber< bits >::Digit().
|
inline |
Compare with a paritally sampled fraction
Random | the type of the random generator. |
[in,out] | r | a random generator. |
[in] | p0 | the starting point for the numerator. |
[in] | c | the stride for the fraction (require c > 0). |
[in] | q | the denominator of the fraction (require q > 0). |
[in,out] | j | the increment for the numerator. |
Definition at line 208 of file RandomNumber.hpp.
References RandomLib::RandomNumber< bits >::Digit(), and RandomLib::UniformInteger< IntType, bits >::GreaterThanEqual().
|
inline |
Random | the type of the random generator. |
[in,out] | r | a random generator. |
[in] | k | the index of a digit of the fraction |
Definition at line 224 of file RandomNumber.hpp.
Referenced by coverage32(), coverage64(), RandomLib::RandomNumber< bits >::Fraction(), RandomLib::RandomNumber< bits >::GreaterPair(), RandomLib::RandomNumber< bits >::LessThan(), and RandomLib::RandomNumber< bits >::Value().
|
inline |
Add one digit to the fraction.
Random | the type of the random generator. |
[in,out] | r | a random generator. |
Definition at line 234 of file RandomNumber.hpp.
References RandomLib::RandomNumber< bits >::RandomDigit().
|
inline |
[in] | k | the index of a digit of the fraction |
std::out_of_range | if the digit hasn't been generated. |
Definition at line 242 of file RandomNumber.hpp.
Referenced by coverage32(), coverage64(), RandomLib::RandomNumber< bits >::Fraction(), RandomLib::RandomNumber< bits >::operator<<(), RandomLib::RandomNumber< bits >::Range(), and RandomLib::RandomNumber< bits >::Value().
|
inline |
[in] | k | the index of a digit of the fraction |
std::out_of_range | if the digit hasn't been generated. |
Definition at line 250 of file RandomNumber.hpp.
|
inline |
Return to initial state, uniformly distributed in n + (0,1). This is similar to Init but also returns the memory used by the object to the system. Normally Init should be used.
Definition at line 257 of file RandomNumber.hpp.
|
inline |
Definition at line 266 of file RandomNumber.hpp.
Referenced by RandomLib::RandomNumber< bits >::AddInteger(), coverage32(), coverage64(), RandomLib::RandomNumber< bits >::operator<<(), and RandomLib::RandomNumber< bits >::Range().
|
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.
RealType | the floating point type to convert to. |
Random | the type of the random generator. |
[in,out] | r | a random generator for generating the necessary digits. |
Definition at line 279 of file RandomNumber.hpp.
References RandomLib::RandomNumber< bits >::Digit(), RandomLib::RandomNumber< bits >::RawDigit(), and STATIC_ASSERT.
Referenced by coverage32(), and coverage64().
|
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.
RealType | the floating point type to convert to. |
Random | the type of the random generator. |
[in,out] | r | a random generator for generating the necessary digits. |
Definition at line 308 of file RandomNumber.hpp.
References RandomLib::RandomNumber< bits >::Digit(), RandomLib::RandomNumber< bits >::RawDigit(), and STATIC_ASSERT.
Referenced by coverage32(), coverage64(), and main().
|
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 360 of file RandomNumber.hpp.
References RandomLib::RandomNumber< bits >::RawDigit(), and RandomLib::RandomNumber< bits >::Size().
Referenced by coverage32(), coverage64(), and main().
|
inlinestatic |
Random | the type of the random generator. |
[in,out] | r | a random generator. |
Definition at line 376 of file RandomNumber.hpp.
Referenced by RandomLib::RandomNumber< bits >::AddDigit().
|
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 437 of file RandomNumber.hpp.
References RandomLib::RandomNumber< bits >::RawDigit(), RandomLib::RandomNumber< bits >::Sign(), RandomLib::RandomNumber< bits >::Size(), and RandomLib::RandomNumber< bits >::UInteger().
|
static |
A mask for the digits.
Definition at line 425 of file RandomNumber.hpp.
Referenced by RandomLib::RandomNumber< bits >::AddInteger().