ExRandom  3.0
unit_uniform_distribution.hpp
Go to the documentation of this file.
1 /**
2  * @file unit_uniform_distribution.hpp
3  * @author Charles Karney <charles.karney@sri.com>
4  * @brief Definition of unit_uniform_distribution
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_DISTRIBUTION_HPP)
11 #define EXRANDOM_UNIT_UNIFORM_DISTRIBUTION_HPP 1
12 
13 #include <iostream> // for std::ostream, etc.
14 #include <limits>
15 
16 #include <exrandom/rand_digit.hpp>
18 
19 namespace exrandom {
20 
21  /**
22  * @brief Sample exactly from the unit uniform distribution.
23  *
24  * This samples from the unit uniform distribution P(x) = 1, for 0 &lt; x
25  * &lt; 1. This is a replacement for std::uniform_real_distribution (with no
26  * parameters). It is equivalent to sampling a real number exactly from the
27  * distribution and rounding it to a floating point number. With the usual
28  * rule for rounding (round to nearest), this means that the end points of
29  * the interval can be returned, i.e., the sampling is from the
30  * <i>closed</i> interval, [0,1].
31  *
32  * @tparam RealType the floating point type of the resulting deviates. This
33  * can include various multi-precision floating point types; see
34  * u_rand::value of details.
35  *
36  * This is a wrapper for unit_uniform_dist to turn it into a C++11 style
37  * random distribution. If the radix of RealType is 2 (the usual case), then
38  * the base for unit_uniform_dist is set to 2<sup>32</sup>; otherwise (e.g.,
39  * RealType is a decimal system), the base is set to the radix.
40  */
41  template<typename RealType = double>
43  public:
44  /**
45  * The type of the range of the distribution.
46  */
47  typedef RealType result_type;
48  /**
49  * @brief Parameter type for unit_uniform_distribution.
50  *
51  * This holds no information because this distribution takes no parameters.
52  */
53  struct param_type {
54  /**
55  * The type of the random number distribution.
56  */
58  };
59 
60  /**
61  * Constructs an uniform distribution.
62  */
63  explicit
64  unit_uniform_distribution() : _uniform_dist(_D) {}
65 
66  /**
67  * Constructs an uniform distribution with a parameter.
68  *
69  * The parameter is ignored because it has no state.
70  */
71  explicit
73  : _uniform_dist(_D) {}
74 
75  /**
76  * Resets the distribution state.
77  */
78  void reset() {}
79 
80  /**
81  * @return the parameter set of the distribution.
82  */
83  param_type param() const { return param_type(); }
84 
85  /**
86  * Sets the parameter set of the distribution.
87  *
88  * The function does not because a param_type has no state.
89  */
90  void param(const param_type& /*param*/) {}
91 
92  /**
93  * @return the greatest lower bound value of the distribution.
94  */
95  result_type min() const { return result_type(0); }
96 
97  /**
98  * @return the least upper bound value of the distribution.
99  */
100  result_type max() const { return result_type(1); }
101 
102  /**
103  * @tparam Generator the type of g.
104  * @param g the random generator engine.
105  * @return an uniform uniform deviate.
106  */
107  template<typename Generator>
109  operator()(Generator& g)
110  { return _uniform_dist.template value<result_type>(g); }
111 
112  /**
113  * @tparam Generator the type of g.
114  * @param g the random generator engine.
115  * @return an uniform uniform deviate.
116  */
117  template<typename Generator>
119  operator()(Generator& g, const param_type& /*p*/)
120  { return this->operator()(g); }
121 
122  /**
123  * Compare two unit_uniform_distributions.
124  * @return true.
125  */
126  friend bool
129  { return true; }
130 
131  /**
132  * Contrast two unit_uniform_distributions.
133  * @return false.
134  */
135  friend bool
138  { return false; }
139 
140  /**
141  * Inserts a unit_uniform_distribution random number distribution into the
142  * output stream @e os.
143  *
144  * @param os an output stream.
145  * @return os.
146  *
147  * This function does nothing because this distribution has no state.
148  */
149  friend std::ostream&
150  operator<<(std::ostream& os,
152  { return os; }
153 
154  /**
155  * Extracts a unit_uniform_distribution random number distribution from the
156  * input stream @e is.
157  *
158  * @param is an input stream.
159  * @return is.
160  *
161  * This function does nothing because this distribution has no state.
162  */
163  friend std::istream&
164  operator>>(std::istream& is, unit_uniform_distribution& /*x*/)
165  { return is; }
166 
167  private:
168  static_assert(!std::numeric_limits<RealType>::is_integer,
169  "template argument not a floating point type");
170  param_type _param;
171  static const uint_t _base = std::numeric_limits<RealType>::radix == 2 ?
172  0UL : std::numeric_limits<RealType>::radix;
175  };
176 
177 }
178 
179 #endif // EXRANDOM_UNIT_UNIFORM_DISTRIBUTION_HPP
Definition of unit_uniform_dist.
friend std::ostream & operator<<(std::ostream &os, const unit_uniform_distribution< RealType > &)
friend bool operator!=(const unit_uniform_distribution< RealType > &, const unit_uniform_distribution< RealType > &)
Sample u-rands exactly from the unit uniform distribution.
friend bool operator==(const unit_uniform_distribution< RealType > &, const unit_uniform_distribution< RealType > &)
Definition of rand_digit.
Parameter type for unit_uniform_distribution.
unit_uniform_distribution< RealType > distribution_type
result_type operator()(Generator &g, const param_type &)
The common namespace.
Definition: aux_info.hpp:18
Sample exactly from the unit uniform distribution.
friend std::istream & operator>>(std::istream &is, unit_uniform_distribution &)