RandomLib::RandomNumber< bits > Class Template Reference

Infinite precision random numbers. More...

#include <RandomLib/RandomNumber.hpp>

List of all members.

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

Detailed Description

template<int bits = 1>
class RandomLib::RandomNumber< bits >

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.


Constructor & Destructor Documentation

template<int bits = 1>
RandomLib::RandomNumber< bits >::RandomNumber ( int  n = 0  )  [inline]

Constructor sets number to n + (0,1).

Definition at line 50 of file RandomNumber.hpp.


Member Function Documentation

template<int bits = 1>
int RandomLib::RandomNumber< bits >::Integer (  )  const [inline]
template<int bits = 1>
template<class Random >
unsigned RandomLib::RandomNumber< bits >::Digit ( Random r,
unsigned  k 
) [inline]
template<int bits = 1>
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().

template<int bits = 1>
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.

template<int bits = 1>
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()().

template<int bits = 1>
void RandomLib::RandomNumber< bits >::Init ( int  n = 0  )  [inline]
template<int bits = 1>
template<class Random >
bool RandomLib::RandomNumber< bits >::LessThan ( Random r,
RandomNumber< bits > &  t 
) [inline]
template<int bits = 1>
void RandomLib::RandomNumber< bits >::Negate (  )  [inline]
template<int bits = 1>
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().

template<int bits = 1>
template<typename RealType , typename Random >
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.

template<int bits = 1>
template<typename RealType , class Random >
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.

template<int bits = 1>
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().

template<int bits = 1>
template<class Random >
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().

template<int bits = 1>
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().


Member Data Documentation

template<int bits = 1>
int RandomLib::RandomNumber< bits >::_n [private]
template<int bits = 1>
std::vector<unsigned> RandomLib::RandomNumber< bits >::_f [private]
template<int bits = 1>
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().


The documentation for this class was generated from the following file:

Generated on Thu Dec 31 15:24:30 2009 by  doxygen 1.6.1