Other random distributions
Back to Other random results. Forward to Saving and restoring the state. 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 RandomLib::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 with 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 8 + 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 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;
  const int b = 32;
  RandomLib::ExactExponential<b> edist;
  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 << std::endl;
  }
Back to Other random results. Forward to Saving and restoring the state. Up to Contents.