RandomLib  1.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
ExactPower.hpp
Go to the documentation of this file.
1 /**
2  * \file ExactPower.hpp
3  * \brief Header for ExactPower
4  *
5  * Sample exactly from a power distribution.
6  *
7  * Copyright (c) Charles Karney (2006-2011) <charles@karney.com> and licensed
8  * under the MIT/X11 License. For more information, see
9  * http://randomlib.sourceforge.net/
10  **********************************************************************/
11 
12 #if !defined(RANDOMLIB_EXACTPOWER_HPP)
13 #define RANDOMLIB_EXACTPOWER_HPP 1
14 
16 
17 namespace RandomLib {
18  /**
19  * \brief Sample exactly from a power distribution.
20  *
21  * Sample exactly from power distribution (<i>n</i> + 1)
22  * <i>x</i><sup><i>n</i></sup> for \e x in (0,1) and integer \e n &ge; 0 using
23  * infinite precision. The template parameter \e bits specifies the number
24  * of bits in the base used for RandomNumber (i.e., base =
25  * 2<sup><i>bits</i></sup>).
26  *
27  * This class uses some mutable RandomNumber objects. So a single ExactPower
28  * object cannot safely be used by multiple threads. In a multi-processing
29  * environment, each thread should use a thread-specific ExactPower object.
30  * In addition, these should be invoked with thread-specific random generator
31  * objects.
32  *
33  * @tparam bits the number of bits in each digit.
34  **********************************************************************/
35  template<int bits = 1> class ExactPower {
36  public:
37  /**
38  * Return the random deviate with a power distribution, (<i>n</i> + 1)
39  * <i>x</i><sup><i>n</i></sup> for \e x in (0,1) and integer \e n &ge; 0.
40  * Returned result is a RandomNumber with base 2<sup><i>bits</i></sup>.
41  * For \e bits = 1, the number of random bits in the result and consumed
42  * are as follows: \verbatim
43  n random bits
44  result consumed
45  0 0 0
46  1 2 4
47  2 2.33 6.67
48  3 2.67 9.24
49  4 2.96 11.71
50  5 3.20 14.11
51  6 3.41 16.45
52  7 3.59 18.75
53  8 3.75 21.01
54  9 3.89 23.25
55  10 4.02 25.47
56  \endverbatim
57  * The relative frequency of the results with \e bits = 1 and \e n = 2 can
58  * be is shown by the histogram
59  * \image html powerhist.png
60  * The base of each rectangle gives the range represented by the
61  * corresponding binary number and the area is proportional to its
62  * frequency. A PDF version of this figure
63  * <a href="powerhist.pdf">here</a>. This allows the figure to be
64  * magnified to show the rectangles for all binary numbers up to 9 bits.
65  *
66  * @tparam Random the type of the random generator.
67  * @param[in,out] r a random generator.
68  * @param[in] n the power.
69  * @return the random sample.
70  **********************************************************************/
71  template<class Random>
72  RandomNumber<bits> operator()(Random& r, unsigned n) const;
73  private:
74  mutable RandomNumber<bits> _x;
75  mutable RandomNumber<bits> _y;
76  };
77 
78  template<int bits> template<class Random> RandomNumber<bits>
79  ExactPower<bits>::operator()(Random& r, unsigned n) const {
80  // Return max(u_0, u_1, u_2, ..., u_n). Equivalent to taking the
81  // (n+1)th root of u_0.
82  _x.Init();
83  for (; n--;) {
84  _y.Init();
85  // For bits = 1, we can save 1 bit on the first iteration by using a
86  // technique suggested by Knuth and Yao (1976). When comparing the
87  // digits of x and y, use 1 bit to determine if the digits are the same.
88  // If they are, then use another bit for the value of the digit. If they
89  // are not, then the next bit of the maximum must be 1 (avoiding using a
90  // second bit). Applying this optimization to subsequent iterations
91  // (when x already is filled with some bits) gets trickier.
92  if (_x.LessThan(r, _y))
93  _x.swap(_y); // x = y;
94  }
95  return _x;
96  }
97 
98 } // namespace RandomLib
99 
100 #endif // RANDOMLIB_EXACTPOWER_HPP
RandomNumber< bits > operator()(Random &r, unsigned n) const
Definition: ExactPower.hpp:79
Header for RandomNumber.
Generate random integers, reals, and booleans.
Infinite precision random numbers.
Sample exactly from a power distribution.
Definition: ExactPower.hpp:35