RandomLib
1.10
|
Generate random integers, reals, and booleans. More...
#include <RandomLib/RandomCanonical.hpp>
Public Types | |
enum | { width = Generator::width } |
typedef Generator::result_type | result_type |
typedef RandomSeed::seed_type | seed_type |
Public Member Functions | |
template<> | |
RandomCanonical < MRandomGenerator32 > | Global |
template<> | |
RandomCanonical < MRandomGenerator64 > | Global |
template<> | |
RandomCanonical < SRandomGenerator32 > | Global |
template<> | |
RandomCanonical < SRandomGenerator64 > | Global |
Constructors which set the seed | |
template<typename IntType > | |
RandomCanonical (const std::vector< IntType > &v) | |
template<typename InputIterator > | |
RandomCanonical (InputIterator a, InputIterator b) | |
RandomCanonical (seed_type n) | |
RandomCanonical () | |
RandomCanonical (const std::string &s) | |
Member functions returning integers | |
result_type | operator() () throw () |
result_type | operator() (result_type n) throw () |
template<typename IntType , int bits> | |
IntType | Integer () throw () |
template<int bits> | |
result_type | Integer () throw () |
template<typename IntType > | |
IntType | Integer () throw () |
result_type | Integer () throw () |
template<typename IntType > | |
IntType | Integer (IntType n) throw () |
template<typename IntType > | |
IntType | IntegerC (IntType n) throw () |
template<typename IntType > | |
IntType | IntegerC (IntType m, IntType n) throw () |
Member functions returning real fixed-point numbers | |
template<typename RealType , int prec> | |
RealType | Fixed () throw () |
template<typename RealType > | |
RealType | Fixed () throw () |
double | Fixed () throw () |
template<typename RealType > | |
RealType | Real () throw () |
double | Real () throw () |
template<typename RealType , int prec> | |
RealType | FixedU () throw () |
template<typename RealType > | |
RealType | FixedU () throw () |
double | FixedU () throw () |
template<typename RealType , int prec> | |
RealType | FixedN () throw () |
template<typename RealType > | |
RealType | FixedN () throw () |
double | FixedN () throw () |
template<typename RealType , int prec> | |
RealType | FixedW () throw () |
template<typename RealType > | |
RealType | FixedW () throw () |
double | FixedW () throw () |
template<typename RealType , int prec> | |
RealType | FixedS () throw () |
template<typename RealType > | |
RealType | FixedS () throw () |
double | FixedS () throw () |
template<typename RealType , int prec> | |
RealType | FixedO () throw () |
template<typename RealType > | |
RealType | FixedO () throw () |
double | FixedO () throw () |
template<typename RealType , int prec> | |
RealType | FixedC () throw () |
template<typename RealType > | |
RealType | FixedC () throw () |
double | FixedC () throw () |
Member functions returning real floating-point numbers | |
template<typename RealType , int prec, int erange> | |
RealType | Float () throw () |
template<typename RealType > | |
RealType | Float () throw () |
double | Float () throw () |
template<typename RealType , int prec, int erange> | |
RealType | FloatU () throw () |
template<typename RealType > | |
RealType | FloatU () throw () |
double | FloatU () throw () |
template<typename RealType , int prec, int erange> | |
RealType | FloatN () throw () |
template<typename RealType > | |
RealType | FloatN () throw () |
double | FloatN () throw () |
template<typename RealType , int prec, int erange> | |
RealType | FloatW () throw () |
template<typename RealType > | |
RealType | FloatW () throw () |
double | FloatW () throw () |
Member functions returning booleans | |
bool | Boolean () throw () |
template<typename NumericType > | |
bool | Prob (NumericType p) throw () |
template<typename NumericType > | |
bool | Prob (NumericType m, NumericType n) throw () |
Functions returning bitsets | |
These return random bits in a std::bitset. | |
template<int nbits> | |
std::bitset< nbits > | Bits () throw () |
Static Public Attributes | |
static RANDOMLIB_EXPORT RandomCanonical | Global |
Generate random integers, reals, and booleans.
Use the random bits from Generator to produce random integers of various sizes, random reals with various precisions, a random probability, etc. RandomCanonical assumes that Generator produces random results as 32-bit quantities (of type uint32_t) via Generator::Ran32(), 64-bit quantities (of type uint64_t) via Generator::Ran64(), and in "natural" units of Generator::width bits (of type Generator::result_type) via Generator::Ran().
For the most part this class uses Ran() when needing width or fewer bits, otherwise it uses Ran64(). However, when width = 64, the resulting code is RandomCanonical::Unsigned(n) is inefficient because of the 64-bit arithmetic. For this reason RandomCanonical::Unsigned(n) uses Ran32() if less than 32 bits are required (even though this results in more numbers being produced by the Generator).
This class has been tested with the 32-bit and 64-bit versions of MT19937 and SFMT19937. Other random number generators could be used provided that they provide a whole number of random bits so that Ran() is uniformly distributed in [0,2w). Probably some modifications would be needed if w is not 32 or 64.
Generator | the type of the underlying generator. |
Definition at line 55 of file RandomCanonical.hpp.
typedef Generator::result_type RandomLib::RandomCanonical< Generator >::result_type |
The type of operator()().
Definition at line 60 of file RandomCanonical.hpp.
typedef RandomSeed::seed_type RandomLib::RandomCanonical< Generator >::seed_type |
The type of elements of Seed().
Definition at line 64 of file RandomCanonical.hpp.
anonymous enum |
Enumerator | |
---|---|
width |
The number of random bits in result_type. |
Definition at line 65 of file RandomCanonical.hpp.
|
inlineexplicit |
Initialize from a vector.
IntType | the integral type of the elements of the vector. |
[in] | v | the vector of elements. |
Definition at line 83 of file RandomCanonical.hpp.
|
inline |
Initialize from a pair of iterator setting seed to [a, b)
InputIterator | the type of the iterator. |
[in] | a | the beginning iterator. |
[in] | b | the ending iterator. |
Definition at line 92 of file RandomCanonical.hpp.
|
explicit |
Initialize with seed [n]
[in] | n | the new seed to use. |
Definition at line 799 of file RandomCanonical.hpp.
References RandomLib::RandomPower2::maxpow, RandomLib::RandomPower2::minpow, RANDOMLIB_HASDENORM, RANDOMLIB_LONGDOUBLEPREC, STATIC_ASSERT, and RandomLib::RandomCanonical< Generator >::width.
|
inline |
Initialize with seed []. This can be followed by a call to Reseed() to select a unique seed.
Definition at line 103 of file RandomCanonical.hpp.
|
inlineexplicit |
Initialize from a string. See RandomCanonical::StringToVector
[in] | s | the string to be decoded into a seed. |
Definition at line 109 of file RandomCanonical.hpp.
|
inline |
Return a raw result in [0, 2w) from the underlying Generator.
Definition at line 122 of file RandomCanonical.hpp.
|
inline |
A random integer in [0, n). This allows a RandomCanonical object to be passed to those standard template library routines that require random numbers. E.g.,
[in] | n | the upper end of the interval. The upper end of the interval is open, so n is never returned. |
Definition at line 138 of file RandomCanonical.hpp.
|
inline |
A random integer of type IntType in [0, 2b).
IntType | the integer type of the returned random numbers. |
bits | how many random bits to return. |
Definition at line 150 of file RandomCanonical.hpp.
References RandomLib::RandomType< bits, UIntType >::mask, STATIC_ASSERT, and RandomLib::RandomCanonical< Generator >::width.
|
inline |
A random integer in [0, 2b).
bits | how many random bits to return. |
Definition at line 173 of file RandomCanonical.hpp.
|
inline |
A random integer of type IntType in [std::numeric_limits<IntType>::min(), std::numeric_limits::max()].
IntType | the integer type of the returned random numbers. |
Definition at line 863 of file RandomCanonical.hpp.
References STATIC_ASSERT, and RandomLib::RandomCanonical< Generator >::width.
|
inline |
A random result_type in [0, std::numeric_limits<result_type>::max()].
Definition at line 189 of file RandomCanonical.hpp.
|
inline |
A random integer of type IntType in [0, n). Excludes n. If n == 0, treat as std::numeric_limits::max() + 1. If n < 0, return 0. Compare RandomCanonical::Integer<int>(0) which returns a result in [0,231) with RandomCanonical::Integer<int>() which returns a result in [−231,231).
IntType | the integer type of the returned random numbers. |
[in] | n | the upper end of the semi-open interval. |
Definition at line 921 of file RandomCanonical.hpp.
References RandomLib::RandomType< bits, UIntType >::mask, and STATIC_ASSERT.
|
inline |
A random integer of type IntType in Closed interval [0, n]. Includes n. If n < 0, return 0.
IntType | the integer type of the returned random numbers. |
[in] | n | the upper end of the closed interval. |
Definition at line 942 of file RandomCanonical.hpp.
References RandomLib::RandomType< bits, UIntType >::mask, and STATIC_ASSERT.
Referenced by main().
|
inline |
A random integer of type IntType in Closed interval [m, n]. Includes both endpoints. If n < m, return m.
IntType | the integer type of the returned random numbers. |
[in] | m | the lower end of the closed interval. |
[in] | n | the upper end of the closed interval. |
Definition at line 958 of file RandomCanonical.hpp.
References STATIC_ASSERT.
|
inline |
In the description of the functions FixedX returning fixed-point numbers, u is a random real number uniformly distributed in (0, 1), p is the precision, and h = 1/2p. Each of the functions come in three variants, e.g.,
See the summary for a comparison of the functions.
Return i h with i in [0,2p) by rounding u down to the previous fixed real. Result is in default interval [0,1).
RealType | the real type of the returned random numbers. |
prec | the precision of the returned random numbers. |
Definition at line 253 of file RandomCanonical.hpp.
References STATIC_ASSERT, and RandomLib::RandomCanonical< Generator >::width.
Referenced by main(), and RandomLib::RandomCanonical< Generator >::Real().
|
inline |
See documentation for RandomCanonical::Fixed<RealType,prec>().
RealType | the real type of the returned random numbers. |
Definition at line 281 of file RandomCanonical.hpp.
|
inline |
See documentation for RandomCanonical::Fixed<RealType,prec>().
Definition at line 288 of file RandomCanonical.hpp.
|
inline |
An alias for RandomCanonical::Fixed<RealType>(). Returns a random number of type RealType in [0,1).
RealType | the real type of the returned random numbers. |
Definition at line 297 of file RandomCanonical.hpp.
|
inline |
An alias for RandomCanonical::Fixed(). Returns a random double in [0,1).
Definition at line 305 of file RandomCanonical.hpp.
References RandomLib::RandomCanonical< Generator >::Fixed().
|
inline |
Return i h with i in (0,2p] by rounding u up to the next fixed real. Result is in upper interval (0,1].
RealType | the real type of the returned random numbers. |
prec | the precision of the returned random numbers. |
Definition at line 316 of file RandomCanonical.hpp.
|
inline |
See documentation for RandomCanonical::FixedU<RealType,prec>().
RealType | the real type of the returned random numbers. |
Definition at line 324 of file RandomCanonical.hpp.
|
inline |
See documentation for RandomCanonical::FixedU<RealType,prec>().
Definition at line 331 of file RandomCanonical.hpp.
|
inline |
Return i h with i in [0,2p] by rounding u to the nearest fixed real. Result is in nearest interval [0,1]. The probability of returning interior values is h while the probability of returning the endpoints is h/2.
RealType | the real type of the returned random numbers. |
prec | the precision of the returned random numbers. |
Definition at line 343 of file RandomCanonical.hpp.
References RandomLib::RandomCanonical< Generator >::Boolean().
|
inline |
See documentation for RandomCanonical::FixedN<RealType,prec>().
RealType | the real type of the returned random numbers. |
Definition at line 353 of file RandomCanonical.hpp.
|
inline |
See documentation for RandomCanonical::FixedN<RealType,prec>().
Definition at line 360 of file RandomCanonical.hpp.
|
inline |
Return i h with i in [−2p, 2p] by rounding 2u − 1 to the nearest fixed real. Result is in wide interval [−1,1]. The probability of returning interior values is h/2 while the probability of returning the endpoints is h/4.
RealType | the real type of the returned random numbers. |
prec | the precision of the returned random numbers. |
Definition at line 373 of file RandomCanonical.hpp.
References RandomLib::RandomCanonical< Generator >::Boolean(), STATIC_ASSERT, and RandomLib::RandomCanonical< Generator >::width.
|
inline |
See documentation for RandomCanonical::FixedW<RealType,prec>().
RealType | the real type of the returned random numbers. |
Definition at line 398 of file RandomCanonical.hpp.
|
inline |
See documentation for RandomCanonical::FixedW<RealType,prec>().
Definition at line 405 of file RandomCanonical.hpp.
|
inline |
Return (i+1/2)h with i in [2p−1, 2p−1) by rounding u − 1/2 to nearest offset fixed real. Result is in symmetric interval (−1/2,1/2).
RealType | the real type of the returned random numbers. |
prec | the precision of the returned random numbers. |
Definition at line 417 of file RandomCanonical.hpp.
Referenced by main().
|
inline |
See documentation for RandomCanonical::FixedS<RealType,prec>().
RealType | the real type of the returned random numbers. |
Definition at line 426 of file RandomCanonical.hpp.
|
inline |
See documentation for RandomCanonical::FixedS<RealType,prec>().
Definition at line 433 of file RandomCanonical.hpp.
|
inline |
Return i h with i in (0,2p) by rounding (1 − h)u up to next fixed real. Result is in open interval (0,1).
RealType | the real type of the returned random numbers. |
prec | the precision of the returned random numbers. |
Definition at line 444 of file RandomCanonical.hpp.
References STATIC_ASSERT.
|
inline |
See documentation for RandomCanonical::FixedO<RealType,prec>().
RealType | the real type of the returned random numbers. |
Definition at line 464 of file RandomCanonical.hpp.
|
inline |
See documentation for RandomCanonical::FixedO<RealType,prec>().
Definition at line 471 of file RandomCanonical.hpp.
|
inline |
Return i h with i in [0,2p] by rounding (1 + h)u down to previous fixed real. Result is in closed interval [0,1].
RealType | the real type of the returned random numbers. |
prec | the precision of the returned random numbers. |
Definition at line 482 of file RandomCanonical.hpp.
References STATIC_ASSERT, and RandomLib::RandomCanonical< Generator >::width.
|
inline |
See documentation for RandomCanonical::FixedC<RealType,prec>().
RealType | the real type of the returned random numbers. |
Definition at line 545 of file RandomCanonical.hpp.
|
inline |
See documentation for RandomCanonical::FixedC<RealType,prec>().
Definition at line 552 of file RandomCanonical.hpp.
|
inline |
In the description of the functions FloatX returning floating-point numbers, u is a random real number uniformly distributed in (0, 1), p is the precision, and e is the exponent range. Each of the functions come in three variants, e.g.,
See the summary for a comparison of the functions.
Return result is in default interval [0,1) by rounding u down to the previous floating real.
RealType | the real type of the returned random numbers. |
prec | the precision of the returned random numbers. |
erange | the exponent range of the returned random numbers. |
Definition at line 586 of file RandomCanonical.hpp.
|
inline |
See documentation for RandomCanonical::Float<RealType,prec,erange>().
RealType | the real type of the returned random numbers. |
Definition at line 594 of file RandomCanonical.hpp.
|
inline |
See documentation for RandomCanonical::Float<RealType,prec,erange>().
Definition at line 603 of file RandomCanonical.hpp.
|
inline |
Return result is in upper interval (0,1] by round u up to the next floating real.
RealType | the real type of the returned random numbers. |
prec | the precision of the returned random numbers. |
erange | the exponent range of the returned random numbers. |
Definition at line 614 of file RandomCanonical.hpp.
|
inline |
See documentation for RandomCanonical::FloatU<RealType,prec,erange>().
RealType | the real type of the returned random numbers. |
Definition at line 622 of file RandomCanonical.hpp.
|
inline |
See documentation for RandomCanonical::FloatU<RealType,prec,erange>().
Definition at line 631 of file RandomCanonical.hpp.
|
inline |
Return result is in nearest interval [0,1] by rounding u to the nearest floating real.
RealType | the real type of the returned random numbers. |
prec | the precision of the returned random numbers. |
erange | the exponent range of the returned random numbers. |
Definition at line 642 of file RandomCanonical.hpp.
References RandomLib::RandomCanonical< Generator >::width.
|
inline |
See documentation for RandomCanonical::FloatN<RealType,prec,erange>().
RealType | the real type of the returned random numbers. |
Definition at line 661 of file RandomCanonical.hpp.
|
inline |
See documentation for RandomCanonical::FloatN<RealType,prec,erange>().
Definition at line 670 of file RandomCanonical.hpp.
|
inline |
Return result is in wide interval [−1,1], by rounding 2u − 1 to the nearest floating real.
RealType | the real type of the returned random numbers. |
prec | the precision of the returned random numbers. |
erange | the exponent range of the returned random numbers. |
Definition at line 682 of file RandomCanonical.hpp.
References RandomLib::RandomCanonical< Generator >::width.
|
inline |
See documentation for RandomCanonical::FloatW<RealType,prec,erange>().
RealType | the real type of the returned random numbers. |
Definition at line 698 of file RandomCanonical.hpp.
|
inline |
See documentation for RandomCanonical::FloatW<RealType,prec,erange>().
Definition at line 707 of file RandomCanonical.hpp.
|
inline |
A coin toss. Equivalent to RandomCanonical::Integer<bool>().
Definition at line 719 of file RandomCanonical.hpp.
Referenced by RandomLib::RandomCanonical< Generator >::FixedN(), RandomLib::RandomCanonical< Generator >::FixedW(), main(), RandomLib::DiscreteNormalAlt< IntType, bits >::operator()(), and RandomLib::DiscreteNormal< IntType >::operator()().
bool RandomLib::RandomCanonical< Generator >::Prob | ( | NumericType | p | ) | |
throw | ( | ||||
) |
The Bernoulli distribution, true with probability p. False if p ≤ 0; true if p ≥ 1. Equivalent to RandomCanonical::Float() < p, but typically faster.
NumericType | the type (integer or real) of the argument. |
[in] | p | the probability. |
Referenced by main().
bool RandomLib::RandomCanonical< Generator >::Prob | ( | NumericType | m, |
NumericType | n | ||
) | |||
throw | ( | ||
) |
True with probability m/n. False if m ≤ 0 or n < 0; true if m ≥ n. With real types, Prob(x, y) is exact but slower than Prob(x/y).
NumericType | the type (integer or real) of the argument. |
[in] | m | the numerator of the probability. |
[in] | n | the denominator of the probability. |
|
inline |
Return nbits random bits
nbits | the number of bits in the bitset. |
Definition at line 1205 of file RandomCanonical.hpp.
References STATIC_ASSERT, and RandomLib::RandomCanonical< Generator >::width.
Referenced by main().
RandomCanonical< MRandomGenerator32 > RandomLib::RandomCanonical< MRandomGenerator32 >::Global | ( | ) |
Definition at line 1397 of file Random.cpp.
RandomCanonical< MRandomGenerator64 > RandomLib::RandomCanonical< MRandomGenerator64 >::Global | ( | ) |
Definition at line 1399 of file Random.cpp.
RandomCanonical< SRandomGenerator32 > RandomLib::RandomCanonical< SRandomGenerator32 >::Global | ( | ) |
Definition at line 1401 of file Random.cpp.
RandomCanonical< SRandomGenerator64 > RandomLib::RandomCanonical< SRandomGenerator64 >::Global | ( | ) |
Definition at line 1403 of file Random.cpp.
|
static |
A "global" random number generator (not thread-safe!), initialized with a fixed seed [].
Definition at line 768 of file RandomCanonical.hpp.