abseil-cpp/absl/random/uniform_real_distribution.h
Go to the documentation of this file.
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 //
15 // -----------------------------------------------------------------------------
16 // File: uniform_real_distribution.h
17 // -----------------------------------------------------------------------------
18 //
19 // This header defines a class for representing a uniform floating-point
20 // distribution over a half-open interval [a,b). You use this distribution in
21 // combination with an Abseil random bit generator to produce random values
22 // according to the rules of the distribution.
23 //
24 // `absl::uniform_real_distribution` is a drop-in replacement for the C++11
25 // `std::uniform_real_distribution` [rand.dist.uni.real] but is considerably
26 // faster than the libstdc++ implementation.
27 //
28 // Note: the standard-library version may occasionally return `1.0` when
29 // default-initialized. See https://bugs.llvm.org//show_bug.cgi?id=18767
30 // `absl::uniform_real_distribution` does not exhibit this behavior.
31 
32 #ifndef ABSL_RANDOM_UNIFORM_REAL_DISTRIBUTION_H_
33 #define ABSL_RANDOM_UNIFORM_REAL_DISTRIBUTION_H_
34 
35 #include <cassert>
36 #include <cmath>
37 #include <cstdint>
38 #include <istream>
39 #include <limits>
40 #include <type_traits>
41 
42 #include "absl/meta/type_traits.h"
43 #include "absl/random/internal/fast_uniform_bits.h"
44 #include "absl/random/internal/generate_real.h"
45 #include "absl/random/internal/iostream_state_saver.h"
46 
47 namespace absl {
49 
50 // absl::uniform_real_distribution<T>
51 //
52 // This distribution produces random floating-point values uniformly distributed
53 // over the half-open interval [a, b).
54 //
55 // Example:
56 //
57 // absl::BitGen gen;
58 //
59 // // Use the distribution to produce a value between 0.0 (inclusive)
60 // // and 1.0 (exclusive).
61 // double value = absl::uniform_real_distribution<double>(0, 1)(gen);
62 //
63 template <typename RealType = double>
64 class uniform_real_distribution {
65  public:
66  using result_type = RealType;
67 
68  class param_type {
69  public:
71 
72  explicit param_type(result_type lo = 0, result_type hi = 1)
73  : lo_(lo), hi_(hi), range_(hi - lo) {
74  // [rand.dist.uni.real] preconditions 2 & 3
75  assert(lo <= hi);
76 
77  // NOTE: For integral types, we can promote the range to an unsigned type,
78  // which gives full width of the range. However for real (fp) types, this
79  // is not possible, so value generation cannot use the full range of the
80  // real type.
82  }
83 
84  result_type a() const { return lo_; }
85  result_type b() const { return hi_; }
86 
87  friend bool operator==(const param_type& a, const param_type& b) {
88  return a.lo_ == b.lo_ && a.hi_ == b.hi_;
89  }
90 
91  friend bool operator!=(const param_type& a, const param_type& b) {
92  return !(a == b);
93  }
94 
95  private:
98 
100  "Class-template absl::uniform_real_distribution<> must be "
101  "parameterized using a floating-point type.");
102  };
103 
105 
107  : param_(lo, hi) {}
108 
110 
111  // uniform_real_distribution<T>::reset()
112  //
113  // Resets the uniform real distribution. Note that this function has no effect
114  // because the distribution already produces independent values.
115  void reset() {}
116 
117  template <typename URBG>
118  result_type operator()(URBG& gen) { // NOLINT(runtime/references)
119  return operator()(gen, param_);
120  }
121 
122  template <typename URBG>
123  result_type operator()(URBG& gen, // NOLINT(runtime/references)
124  const param_type& p);
125 
126  result_type a() const { return param_.a(); }
127  result_type b() const { return param_.b(); }
128 
129  param_type param() const { return param_; }
130  void param(const param_type& params) { param_ = params; }
131 
132  result_type(min)() const { return a(); }
133  result_type(max)() const { return b(); }
134 
136  const uniform_real_distribution& b) {
137  return a.param_ == b.param_;
138  }
140  const uniform_real_distribution& b) {
141  return a.param_ != b.param_;
142  }
143 
144  private:
147 };
148 
149 // -----------------------------------------------------------------------------
150 // Implementation details follow
151 // -----------------------------------------------------------------------------
152 template <typename RealType>
153 template <typename URBG>
156  URBG& gen, const param_type& p) { // NOLINT(runtime/references)
159  using real_type =
161 
162  while (true) {
163  const result_type sample =
164  GenerateRealFromBits<real_type, GeneratePositiveTag, true>(
165  fast_u64_(gen));
166  const result_type res = p.a() + (sample * p.range_);
167  if (res < p.b() || p.range_ <= 0 || !std::isfinite(p.range_)) {
168  return res;
169  }
170  // else sample rejected, try again.
171  }
172 }
173 
174 template <typename CharT, typename Traits, typename RealType>
175 std::basic_ostream<CharT, Traits>& operator<<(
176  std::basic_ostream<CharT, Traits>& os, // NOLINT(runtime/references)
180  os << x.a() << os.fill() << x.b();
181  return os;
182 }
183 
184 template <typename CharT, typename Traits, typename RealType>
185 std::basic_istream<CharT, Traits>& operator>>(
186  std::basic_istream<CharT, Traits>& is, // NOLINT(runtime/references)
187  uniform_real_distribution<RealType>& x) { // NOLINT(runtime/references)
188  using param_type = typename uniform_real_distribution<RealType>::param_type;
191  auto a = random_internal::read_floating_point<result_type>(is);
192  if (is.fail()) return is;
193  auto b = random_internal::read_floating_point<result_type>(is);
194  if (!is.fail()) {
195  x.param(param_type(a, b));
196  }
197  return is;
198 }
200 } // namespace absl
201 
202 #endif // ABSL_RANDOM_UNIFORM_REAL_DISTRIBUTION_H_
absl::uniform_real_distribution::min
result_type() min() const
Definition: abseil-cpp/absl/random/uniform_real_distribution.h:132
absl::uniform_real_distribution::uniform_real_distribution
uniform_real_distribution(result_type lo, result_type hi=1)
Definition: abseil-cpp/absl/random/uniform_real_distribution.h:106
absl::uniform_real_distribution::operator()
result_type operator()(URBG &gen)
Definition: abseil-cpp/absl/random/uniform_real_distribution.h:118
absl::uniform_real_distribution::max
result_type() max() const
Definition: abseil-cpp/absl/random/uniform_real_distribution.h:133
absl::uniform_real_distribution::param_type::hi_
result_type hi_
Definition: abseil-cpp/absl/random/uniform_real_distribution.h:97
absl::uniform_real_distribution::result_type
RealType result_type
Definition: abseil-cpp/absl/random/uniform_real_distribution.h:66
absl::random_internal::make_istream_state_saver
istream_state_saver< std::basic_istream< CharT, Traits > > make_istream_state_saver(std::basic_istream< CharT, Traits > &is, std::ios_base::fmtflags flags=std::ios_base::dec|std::ios_base::scientific|std::ios_base::skipws)
Definition: abseil-cpp/absl/random/internal/iostream_state_saver.h:149
absl::operator<<
ABSL_NAMESPACE_BEGIN std::ostream & operator<<(std::ostream &os, absl::LogSeverity s)
Definition: abseil-cpp/absl/base/log_severity.cc:24
google::protobuf.internal::real_type
FieldDescriptor::Type real_type(FieldType type)
Definition: bloaty/third_party/protobuf/src/google/protobuf/extension_set_heavy.cc:128
absl::conditional_t
typename std::conditional< B, T, F >::type conditional_t
Definition: abseil-cpp/absl/meta/type_traits.h:634
absl::uniform_real_distribution::param_type::a
result_type a() const
Definition: abseil-cpp/absl/random/uniform_real_distribution.h:84
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
absl::random_internal::GeneratePositiveTag
Definition: abseil-cpp/absl/random/internal/generate_real.h:36
absl::uniform_real_distribution::uniform_real_distribution
uniform_real_distribution()
Definition: abseil-cpp/absl/random/uniform_real_distribution.h:104
absl::uniform_real_distribution::param_type::range_
result_type range_
Definition: abseil-cpp/absl/random/uniform_real_distribution.h:97
xds_manager.p
p
Definition: xds_manager.py:60
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
absl::uniform_real_distribution::param
param_type param() const
Definition: abseil-cpp/absl/random/uniform_real_distribution.h:129
absl::random_internal::GenerateRealFromBits
RealType GenerateRealFromBits(uint64_t bits, int exp_bias=0)
Definition: abseil-cpp/absl/random/internal/generate_real.h:70
absl::uniform_real_distribution::param_type::b
result_type b() const
Definition: abseil-cpp/absl/random/uniform_real_distribution.h:85
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
absl::uniform_real_distribution::b
result_type b() const
Definition: abseil-cpp/absl/random/uniform_real_distribution.h:127
absl::uniform_real_distribution::param_type::uniform_real_distribution
friend class uniform_real_distribution
Definition: abseil-cpp/absl/random/uniform_real_distribution.h:96
result_type
const typedef int * result_type
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:4325
absl::uniform_real_distribution::param_type::param_type
param_type(result_type lo=0, result_type hi=1)
Definition: abseil-cpp/absl/random/uniform_real_distribution.h:72
absl::uniform_real_distribution::fast_u64_
random_internal::FastUniformBits< uint64_t > fast_u64_
Definition: abseil-cpp/absl/random/uniform_real_distribution.h:146
absl::uniform_real_distribution::a
result_type a() const
Definition: abseil-cpp/absl/random/uniform_real_distribution.h:126
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
absl::uniform_real_distribution
Definition: abseil-cpp/absl/random/internal/uniform_helper.h:33
gen
OPENSSL_EXPORT GENERAL_NAME * gen
Definition: x509v3.h:495
absl::uniform_real_distribution::param_type::operator!=
friend bool operator!=(const param_type &a, const param_type &b)
Definition: abseil-cpp/absl/random/uniform_real_distribution.h:91
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
absl::random_internal::stream_precision_helper
Definition: abseil-cpp/absl/random/internal/iostream_state_saver.h:107
absl::uniform_real_distribution::param
void param(const param_type &params)
Definition: abseil-cpp/absl/random/uniform_real_distribution.h:130
value
const char * value
Definition: hpack_parser_table.cc:165
absl::operator>>
constexpr uint128 operator>>(uint128 lhs, int amount)
Definition: abseil-cpp/absl/numeric/int128.h:917
absl::uniform_real_distribution::param_
param_type param_
Definition: abseil-cpp/absl/random/uniform_real_distribution.h:145
absl::uniform_real_distribution::uniform_real_distribution
uniform_real_distribution(const param_type &param)
Definition: abseil-cpp/absl/random/uniform_real_distribution.h:109
absl::uniform_real_distribution::operator!=
friend bool operator!=(const uniform_real_distribution &a, const uniform_real_distribution &b)
Definition: abseil-cpp/absl/random/uniform_real_distribution.h:139
absl::uniform_real_distribution::param_type
Definition: abseil-cpp/absl/random/uniform_real_distribution.h:68
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
absl::uniform_real_distribution::reset
void reset()
Definition: abseil-cpp/absl/random/uniform_real_distribution.h:115
isfinite
#define isfinite
Definition: bloaty/third_party/protobuf/conformance/third_party/jsoncpp/jsoncpp.cpp:4014
absl::random_internal::make_ostream_state_saver
ostream_state_saver< std::basic_ostream< CharT, Traits > > make_ostream_state_saver(std::basic_ostream< CharT, Traits > &os, std::ios_base::fmtflags flags=std::ios_base::dec|std::ios_base::left|std::ios_base::scientific)
Definition: abseil-cpp/absl/random/internal/iostream_state_saver.h:82
absl::uniform_real_distribution::param_type::operator==
friend bool operator==(const param_type &a, const param_type &b)
Definition: abseil-cpp/absl/random/uniform_real_distribution.h:87
absl::uniform_real_distribution::param_type::lo_
result_type lo_
Definition: abseil-cpp/absl/random/uniform_real_distribution.h:97
absl::random_internal::FastUniformBits< uint64_t >
absl::uniform_real_distribution::operator==
friend bool operator==(const uniform_real_distribution &a, const uniform_real_distribution &b)
Definition: abseil-cpp/absl/random/uniform_real_distribution.h:135


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:44