RandomLib  1.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
Other random distributions
Back to Other random results. Forward to MPFR interface. Up to Contents.

This library includes implementation of a few other random distributions

These provide fast, reasonably accurate (or, where noted, exact) implementations of these distributions.

In general, it is difficult to sample from an distribution and round the result exactly to the nearest representable real number. However, for some simple distributions, the exact distribution is given by a series of uniform distributions and this provides a method for sampling exactly. The class which represents the result of sampling from such distributions is RandomNumber. It can be thought of as an infinite precision random number. At any time only some of the leading digits have been computed and the result then stands for any number obtained by filling in the remaining digits uniformly and randomly. The following are distributions which return a RandomNumber

No attempt has been to optimize these exact distributions for speed. However, with base = 2, ExactExponential delivers k bits of accuracy consuming, on average, only about 5.6 + k bits of randomness; so a fast implementation is possible. (Similarly it's possible to return true with probability 1/e consuming 6 bits of randomness.)

The algorithms used by ExactExponential and ExactNormal have been included in MPFR. See also MPFR interface.

The following code samples from the exponential distribution, rounding the results exactly to the nearest double. Thus the probability that 0.75 is returned is exactly 2 exp(−3/4) sinh(2−54) (with no error in the evaluation of exp and sinh). (This assumes, of course, that the underlying random number generator is perfect.)

RandomLib::Random r; r.Reseed();
const int b = 32;
for (size_t i = 0; i < 10; ++i) {
RandomLib::RandomNumber<b> x = edist(r); // Sample exactly
double y = x.Value<double>(r); // Round exactly to nearest double
std::cout << y << "\n";

A more extensive example of generating exact distributions is given by RandomExact.cpp.

Back to Other random results. Forward to MPFR interface. Up to Contents.