RandomLib  1.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
Public Member Functions | Static Public Member Functions | Static Public Attributes | Related Functions | List of all members
RandomLib::RandomNumber< bits > Class Template Reference

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)
 

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 ∑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.)

Template Parameters
bitsthe number of bits in each digit.

Definition at line 41 of file RandomNumber.hpp.

Constructor & Destructor Documentation

template<int bits = 1>
RandomLib::RandomNumber< bits >::RandomNumber ( )
throw (
)
inline

Constructor sets number to a random number uniformly distributed in (0,1).

Definition at line 47 of file RandomNumber.hpp.

Member Function Documentation

template<int bits = 1>
void RandomLib::RandomNumber< bits >::swap ( RandomNumber< bits > &  t)
throw (
)
inline

Swap with another RandomNumber. This is a fast way of doing an assignment.

Parameters
[in,out]tthe RandomNumber to swap with.

Definition at line 54 of file RandomNumber.hpp.

References std::swap().

template<int bits = 1>
void RandomLib::RandomNumber< bits >::Init ( )
throw (
)
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().

template<int bits = 1>
int RandomLib::RandomNumber< bits >::Sign ( ) const
throw (
)
inline
Returns
the sign of the RandomNumber (± 1).

Definition at line 74 of file RandomNumber.hpp.

Referenced by coverage32(), coverage64(), and RandomLib::RandomNumber< bits >::operator<<().

template<int bits = 1>
void RandomLib::RandomNumber< bits >::Negate ( )
throw (
)
inline

Change the sign of the RandomNumber.

Definition at line 78 of file RandomNumber.hpp.

Referenced by coverage32(), and coverage64().

template<int bits = 1>
int RandomLib::RandomNumber< bits >::Floor ( ) const
throw (
)
inline
Returns
the floor of the RandomNumber.

Definition at line 82 of file RandomNumber.hpp.

Referenced by RandomLib::RandomNumber< bits >::AddInteger(), coverage32(), and coverage64().

template<int bits = 1>
int RandomLib::RandomNumber< bits >::Ceiling ( ) const
throw (
)
inline
Returns
the ceiling of the RandomNumber.

Definition at line 86 of file RandomNumber.hpp.

Referenced by coverage32(), and coverage64().

template<int bits = 1>
unsigned RandomLib::RandomNumber< bits >::UInteger ( ) const
throw (
)
inline
Returns
the unsigned integer component of the RandomNumber.

Definition at line 90 of file RandomNumber.hpp.

Referenced by RandomLib::RandomNumber< bits >::operator<<().

template<int bits = 1>
void RandomLib::RandomNumber< bits >::AddInteger ( int  k)
throw (
)
inline

Add integer k to the RandomNumber.

Parameters
[in]kthe 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().

template<int bits = 1>
template<class Random >
bool RandomLib::RandomNumber< bits >::LessThan ( Random r,
RandomNumber< bits > &  t 
)
inline

Compare with another RandomNumber, *this < t

Template Parameters
Randomthe type of the random generator.
Parameters
[in,out]ra random generator.
[in,out]ta RandomNumber to compare.
Returns
true if *this < t.

Definition at line 112 of file RandomNumber.hpp.

References RandomLib::RandomNumber< bits >::Digit().

Referenced by coverage32(), and coverage64().

template<int bits = 1>
template<class Random >
bool RandomLib::RandomNumber< bits >::GreaterPair ( Random r,
RandomNumber< bits > &  u,
RandomNumber< bits > &  v 
)
inline

Compare RandomNumber with two others, *this > max(u, v)

Template Parameters
Randomthe type of the random generator.
Parameters
[in,out]ra random generator.
[in,out]ufirst RandomNumber to compare.
[in,out]vsecond RandomNumber to compare.
Returns
true if *this > max(u, v).

Definition at line 133 of file RandomNumber.hpp.

References RandomLib::RandomNumber< bits >::Digit().

template<int bits = 1>
template<class Random , typename IntType >
bool RandomLib::RandomNumber< bits >::LessThan ( Random r,
IntType  p,
IntType  q 
)
inline

Compare with a fraction, *this < p/q

Template Parameters
Randomthe type of the random generator.
Parameters
[in,out]ra random generator.
[in]pthe numerator of the fraction.
[in]qthe denominator of the fraction (require q > 0).
Returns
true if *this < p/q.

Definition at line 187 of file RandomNumber.hpp.

References RandomLib::RandomNumber< bits >::Digit().

template<int bits = 1>
template<class Random , typename IntType >
bool RandomLib::RandomNumber< bits >::LessThan ( Random r,
IntType  p0,
IntType  c,
IntType  q,
UniformInteger< IntType, bits > &  j 
)
inline

Compare with a paritally sampled fraction

Template Parameters
Randomthe type of the random generator.
Parameters
[in,out]ra random generator.
[in]p0the starting point for the numerator.
[in]cthe stride for the fraction (require c > 0).
[in]qthe denominator of the fraction (require q > 0).
[in,out]jthe increment for the numerator.
Returns
true if *this < (p0 + cj)/q.

Definition at line 208 of file RandomNumber.hpp.

References RandomLib::RandomNumber< bits >::Digit(), and RandomLib::UniformInteger< IntType, bits >::GreaterThanEqual().

template<int bits = 1>
template<class Random >
unsigned RandomLib::RandomNumber< bits >::Digit ( Random r,
unsigned  k 
)
inline
Template Parameters
Randomthe type of the random generator.
Parameters
[in,out]ra random generator.
[in]kthe index of a digit of the fraction
Returns
digit number k, generating it if necessary.

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().

template<int bits = 1>
template<class Random >
void RandomLib::RandomNumber< bits >::AddDigit ( Random r)
inline

Add one digit to the fraction.

Template Parameters
Randomthe type of the random generator.
Parameters
[in,out]ra random generator.

Definition at line 234 of file RandomNumber.hpp.

References RandomLib::RandomNumber< bits >::RandomDigit().

template<int bits = 1>
const unsigned& RandomLib::RandomNumber< bits >::RawDigit ( unsigned  k) const
throw (
)
inline
Parameters
[in]kthe index of a digit of the fraction
Returns
a const reference to digit number k, without generating new digits.
Exceptions
std::out_of_rangeif 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().

template<int bits = 1>
unsigned& RandomLib::RandomNumber< bits >::RawDigit ( unsigned  k)
throw (
)
inline
Parameters
[in]kthe index of a digit of the fraction
Returns
a non-const reference to digit number k, without generating new digits.
Exceptions
std::out_of_rangeif the digit hasn't been generated.

Definition at line 250 of file RandomNumber.hpp.

template<int bits = 1>
void RandomLib::RandomNumber< bits >::Clear ( )
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.

template<int bits = 1>
unsigned RandomLib::RandomNumber< bits >::Size ( ) const
throw (
)
inline
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.

Template Parameters
RealTypethe floating point type to convert to.
Randomthe type of the random generator.
Parameters
[in,out]ra random generator for generating the necessary digits.
Returns
the fraction of the RandomNumber rounded to a RealType.

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().

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.

Template Parameters
RealTypethe floating point type to convert to.
Randomthe type of the random generator.
Parameters
[in,out]ra random generator for generating the necessary digits.
Returns
the value of the RandomNumber rounded to a RealType.

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().

template<int bits = 1>
std::pair<double, double> RandomLib::RandomNumber< bits >::Range ( ) const
throw (
)
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.

Returns
a pair denoting the range with first being the lower limit and second being the upper limit.

Definition at line 360 of file RandomNumber.hpp.

References RandomLib::RandomNumber< bits >::RawDigit(), and RandomLib::RandomNumber< bits >::Size().

Referenced by coverage32(), coverage64(), and main().

template<int bits = 1>
template<class Random >
static unsigned RandomLib::RandomNumber< bits >::RandomDigit ( Random r)
throw (
)
inlinestatic
Template Parameters
Randomthe type of the random generator.
Parameters
[in,out]ra random generator.
Returns
a random digit in [0, 2bits).

Definition at line 376 of file RandomNumber.hpp.

Referenced by RandomLib::RandomNumber< bits >::AddDigit().

Friends And Related Function Documentation

template<int bits>
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 437 of file RandomNumber.hpp.

References RandomLib::RandomNumber< bits >::RawDigit(), RandomLib::RandomNumber< bits >::Sign(), RandomLib::RandomNumber< bits >::Size(), and RandomLib::RandomNumber< bits >::UInteger().

Member Data Documentation

template<int bits = 1>
const unsigned RandomLib::RandomNumber< bits >::mask
static
Initial value:
=
bits == w ? ~0U : ~(~0U << (bits < w ? bits : 0))

A mask for the digits.

Definition at line 425 of file RandomNumber.hpp.

Referenced by RandomLib::RandomNumber< bits >::AddInteger().


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