log_uniform_int_distribution.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef ABSL_RANDOM_LOG_UNIFORM_INT_DISTRIBUTION_H_
  15. #define ABSL_RANDOM_LOG_UNIFORM_INT_DISTRIBUTION_H_
  16. #include <algorithm>
  17. #include <cassert>
  18. #include <cmath>
  19. #include <istream>
  20. #include <limits>
  21. #include <ostream>
  22. #include "absl/base/config.h"
  23. #include "absl/random/internal/iostream_state_saver.h"
  24. #include "absl/random/internal/traits.h"
  25. #include "absl/random/uniform_int_distribution.h"
  26. namespace absl {
  27. ABSL_NAMESPACE_BEGIN
  28. // log_uniform_int_distribution:
  29. //
  30. // Returns a random variate R in range [min, max] such that
  31. // floor(log(R-min, base)) is uniformly distributed.
  32. // We ensure uniformity by discretization using the
  33. // boundary sets [0, 1, base, base * base, ... min(base*n, max)]
  34. //
  35. template <typename IntType = int>
  36. class log_uniform_int_distribution {
  37. private:
  38. using unsigned_type =
  39. typename random_internal::make_unsigned_bits<IntType>::type;
  40. public:
  41. using result_type = IntType;
  42. class param_type {
  43. public:
  44. using distribution_type = log_uniform_int_distribution;
  45. explicit param_type(
  46. result_type min = 0,
  47. result_type max = (std::numeric_limits<result_type>::max)(),
  48. result_type base = 2)
  49. : min_(min),
  50. max_(max),
  51. base_(base),
  52. range_(static_cast<unsigned_type>(max_) -
  53. static_cast<unsigned_type>(min_)),
  54. log_range_(0) {
  55. assert(max_ >= min_);
  56. assert(base_ > 1);
  57. if (base_ == 2) {
  58. // Determine where the first set bit is on range(), giving a log2(range)
  59. // value which can be used to construct bounds.
  60. log_range_ = (std::min)(random_internal::BitWidth(range()),
  61. std::numeric_limits<unsigned_type>::digits);
  62. } else {
  63. // NOTE: Computing the logN(x) introduces error from 2 sources:
  64. // 1. Conversion of int to double loses precision for values >=
  65. // 2^53, which may cause some log() computations to operate on
  66. // different values.
  67. // 2. The error introduced by the division will cause the result
  68. // to differ from the expected value.
  69. //
  70. // Thus a result which should equal K may equal K +/- epsilon,
  71. // which can eliminate some values depending on where the bounds fall.
  72. const double inv_log_base = 1.0 / std::log(static_cast<double>(base_));
  73. const double log_range = std::log(static_cast<double>(range()) + 0.5);
  74. log_range_ = static_cast<int>(std::ceil(inv_log_base * log_range));
  75. }
  76. }
  77. result_type(min)() const { return min_; }
  78. result_type(max)() const { return max_; }
  79. result_type base() const { return base_; }
  80. friend bool operator==(const param_type& a, const param_type& b) {
  81. return a.min_ == b.min_ && a.max_ == b.max_ && a.base_ == b.base_;
  82. }
  83. friend bool operator!=(const param_type& a, const param_type& b) {
  84. return !(a == b);
  85. }
  86. private:
  87. friend class log_uniform_int_distribution;
  88. int log_range() const { return log_range_; }
  89. unsigned_type range() const { return range_; }
  90. result_type min_;
  91. result_type max_;
  92. result_type base_;
  93. unsigned_type range_; // max - min
  94. int log_range_; // ceil(logN(range_))
  95. static_assert(random_internal::IsIntegral<IntType>::value,
  96. "Class-template absl::log_uniform_int_distribution<> must be "
  97. "parameterized using an integral type.");
  98. };
  99. log_uniform_int_distribution() : log_uniform_int_distribution(0) {}
  100. explicit log_uniform_int_distribution(
  101. result_type min,
  102. result_type max = (std::numeric_limits<result_type>::max)(),
  103. result_type base = 2)
  104. : param_(min, max, base) {}
  105. explicit log_uniform_int_distribution(const param_type& p) : param_(p) {}
  106. void reset() {}
  107. // generating functions
  108. template <typename URBG>
  109. result_type operator()(URBG& g) { // NOLINT(runtime/references)
  110. return (*this)(g, param_);
  111. }
  112. template <typename URBG>
  113. result_type operator()(URBG& g, // NOLINT(runtime/references)
  114. const param_type& p) {
  115. return static_cast<result_type>((p.min)() + Generate(g, p));
  116. }
  117. result_type(min)() const { return (param_.min)(); }
  118. result_type(max)() const { return (param_.max)(); }
  119. result_type base() const { return param_.base(); }
  120. param_type param() const { return param_; }
  121. void param(const param_type& p) { param_ = p; }
  122. friend bool operator==(const log_uniform_int_distribution& a,
  123. const log_uniform_int_distribution& b) {
  124. return a.param_ == b.param_;
  125. }
  126. friend bool operator!=(const log_uniform_int_distribution& a,
  127. const log_uniform_int_distribution& b) {
  128. return a.param_ != b.param_;
  129. }
  130. private:
  131. // Returns a log-uniform variate in the range [0, p.range()]. The caller
  132. // should add min() to shift the result to the correct range.
  133. template <typename URNG>
  134. unsigned_type Generate(URNG& g, // NOLINT(runtime/references)
  135. const param_type& p);
  136. param_type param_;
  137. };
  138. template <typename IntType>
  139. template <typename URBG>
  140. typename log_uniform_int_distribution<IntType>::unsigned_type
  141. log_uniform_int_distribution<IntType>::Generate(
  142. URBG& g, // NOLINT(runtime/references)
  143. const param_type& p) {
  144. // sample e over [0, log_range]. Map the results of e to this:
  145. // 0 => 0
  146. // 1 => [1, b-1]
  147. // 2 => [b, (b^2)-1]
  148. // n => [b^(n-1)..(b^n)-1]
  149. const int e = absl::uniform_int_distribution<int>(0, p.log_range())(g);
  150. if (e == 0) {
  151. return 0;
  152. }
  153. const int d = e - 1;
  154. unsigned_type base_e, top_e;
  155. if (p.base() == 2) {
  156. base_e = static_cast<unsigned_type>(1) << d;
  157. top_e = (e >= std::numeric_limits<unsigned_type>::digits)
  158. ? (std::numeric_limits<unsigned_type>::max)()
  159. : (static_cast<unsigned_type>(1) << e) - 1;
  160. } else {
  161. const double r = std::pow(static_cast<double>(p.base()), d);
  162. const double s = (r * static_cast<double>(p.base())) - 1.0;
  163. base_e =
  164. (r > static_cast<double>((std::numeric_limits<unsigned_type>::max)()))
  165. ? (std::numeric_limits<unsigned_type>::max)()
  166. : static_cast<unsigned_type>(r);
  167. top_e =
  168. (s > static_cast<double>((std::numeric_limits<unsigned_type>::max)()))
  169. ? (std::numeric_limits<unsigned_type>::max)()
  170. : static_cast<unsigned_type>(s);
  171. }
  172. const unsigned_type lo = (base_e >= p.range()) ? p.range() : base_e;
  173. const unsigned_type hi = (top_e >= p.range()) ? p.range() : top_e;
  174. // choose uniformly over [lo, hi]
  175. return absl::uniform_int_distribution<result_type>(
  176. static_cast<result_type>(lo), static_cast<result_type>(hi))(g);
  177. }
  178. template <typename CharT, typename Traits, typename IntType>
  179. std::basic_ostream<CharT, Traits>& operator<<(
  180. std::basic_ostream<CharT, Traits>& os, // NOLINT(runtime/references)
  181. const log_uniform_int_distribution<IntType>& x) {
  182. using stream_type =
  183. typename random_internal::stream_format_type<IntType>::type;
  184. auto saver = random_internal::make_ostream_state_saver(os);
  185. os << static_cast<stream_type>((x.min)()) << os.fill()
  186. << static_cast<stream_type>((x.max)()) << os.fill()
  187. << static_cast<stream_type>(x.base());
  188. return os;
  189. }
  190. template <typename CharT, typename Traits, typename IntType>
  191. std::basic_istream<CharT, Traits>& operator>>(
  192. std::basic_istream<CharT, Traits>& is, // NOLINT(runtime/references)
  193. log_uniform_int_distribution<IntType>& x) { // NOLINT(runtime/references)
  194. using param_type = typename log_uniform_int_distribution<IntType>::param_type;
  195. using result_type =
  196. typename log_uniform_int_distribution<IntType>::result_type;
  197. using stream_type =
  198. typename random_internal::stream_format_type<IntType>::type;
  199. stream_type min;
  200. stream_type max;
  201. stream_type base;
  202. auto saver = random_internal::make_istream_state_saver(is);
  203. is >> min >> max >> base;
  204. if (!is.fail()) {
  205. x.param(param_type(static_cast<result_type>(min),
  206. static_cast<result_type>(max),
  207. static_cast<result_type>(base)));
  208. }
  209. return is;
  210. }
  211. ABSL_NAMESPACE_END
  212. } // namespace absl
  213. #endif // ABSL_RANDOM_LOG_UNIFORM_INT_DISTRIBUTION_H_