ExRandom  3.0
unit_uniform_dist.hpp
Go to the documentation of this file.
1 /**
2  * @file unit_uniform_dist.hpp
3  * @author Charles Karney <charles.karney@sri.com>
4  * @brief Definition of unit_uniform_dist
5  *
6  * Copyright (c) Charles Karney (2014) and licensed under the MIT/X11 License.
7  * For more information, see http://exrandom.sourceforge.net/
8  */
9 
10 #if !defined(EXRANDOM_UNIT_UNIFORM_DIST_HPP)
11 #define EXRANDOM_UNIT_UNIFORM_DIST_HPP 1
12 
13 #include <exrandom/u_rand.hpp>
14 
15 namespace exrandom {
16 
17  /**
18  * @brief Sample u-rands exactly from the unit uniform distribution
19  *
20  * This samples from the unit uniform distribution P(x) = 1, for 0 &lt; x
21  * &lt; 1. This is a trivial call to the u_rand class.
22  *
23  * @tparam digit_gen the type of digit generator.
24  *
25  * This class allows a u-rand to be returned via the
26  * unit_uniform_dist::generate member function or a floating point
27  * result via the unit_uniform_dist::value<RealType> member function.
28  *
29  * See unit_uniform_distribution for a simpler interface to sampling
30  * uniform deviates. (But, you can't obtain u-rands with this class and
31  * the constructor locks you into a specific floating point type.)
32  */
33  template<typename digit_gen>
35  public:
36  /**
37  * The constructor.
38  *
39  * @param D a reference to the digit generator to be used.
40  */
41  unit_uniform_dist(digit_gen& D) : _D(D), _x(D) {}
42 
43  /**
44  * Generate the next deviate as a u_rand.
45  *
46  * @tparam Generator the type of generator.
47  * @param[out] x the u_rand to set.
48  */
49  template<typename Generator>
50  void generate(Generator& /*g*/, u_rand<digit_gen>& x)
51  { x.init(); return; }
52 
53  /**
54  * Generate the next deviate and round it to a floating point number.
55  *
56  * @tparam RealType the floating point type of the result.
57  * @tparam Generator the type of g.
58  * @param g the random generator engine.
59  * @return the uniform deviate.
60  */
61  template<typename RealType, typename Generator>
62  RealType value(Generator& g) {
63  generate(g, _x);
64  return _x.template value<RealType>(g);
65  }
66 
67  /**
68  * @return a reference to the digit generator used in the constructor.
69  */
70  digit_gen& digit_generator() const { return _D; }
71 
72  /**
73  * The base of the digit generator
74  */
75  static const uint_t base = digit_gen::base;
76  private:
77  static const uint_t bm1 = digit_gen::max_value;
78  // Disable copy assignment
79  unit_uniform_dist& operator=(const unit_uniform_dist&);
80  digit_gen& _D;
81  u_rand<digit_gen> _x; // temporary storage
82  };
83 
84 }
85 
86 #endif // EXRANDOM_UNIT_UNIFORM_DIST_HPP
Sample u-rands exactly from the unit uniform distribution.
digit_gen & digit_generator() const
Definition of u_rand.
u_rand & init()
Definition: u_rand.hpp:123
The common namespace.
Definition: aux_info.hpp:18
The machinery to handle u-rands, arbitrary precision random deviates.
Definition: u_rand.hpp:105
void generate(Generator &, u_rand< digit_gen > &x)
RealType value(Generator &g)