1 // Copyright 2017 The Abseil Authors.
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
7 // https://www.apache.org/licenses/LICENSE-2.0
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.
15 // -----------------------------------------------------------------------------
16 // File: uniform_real_distribution.h
17 // -----------------------------------------------------------------------------
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.
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.
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.
32 #ifndef ABSL_RANDOM_UNIFORM_REAL_DISTRIBUTION_H_
33 #define ABSL_RANDOM_UNIFORM_REAL_DISTRIBUTION_H_
40 #include <type_traits>
42 #include "absl/random/internal/distribution_impl.h"
43 #include "absl/random/internal/fast_uniform_bits.h"
44 #include "absl/random/internal/iostream_state_saver.h"
48 // absl::uniform_real_distribution<T>
50 // This distribution produces random floating-point values uniformly distributed
51 // over the half-open interval [a, b).
57 // // Use the distribution to produce a value between 0.0 (inclusive)
58 // // and 1.0 (exclusive).
59 // int value = absl::uniform_real_distribution<double>(0, 1)(gen);
61 template <typename RealType = double>
62 class uniform_real_distribution {
64 using result_type = RealType;
68 using distribution_type = uniform_real_distribution;
70 explicit param_type(result_type lo = 0, result_type hi = 1)
71 : lo_(lo), hi_(hi), range_(hi - lo) {
72 // [rand.dist.uni.real] preconditions 2 & 3
74 // NOTE: For integral types, we can promote the range to an unsigned type,
75 // which gives full width of the range. However for real (fp) types, this
76 // is not possible, so value generation cannot use the full range of the
78 assert(range_ <= (std::numeric_limits<result_type>::max)());
81 result_type a() const { return lo_; }
82 result_type b() const { return hi_; }
84 friend bool operator==(const param_type& a, const param_type& b) {
85 return a.lo_ == b.lo_ && a.hi_ == b.hi_;
88 friend bool operator!=(const param_type& a, const param_type& b) {
93 friend class uniform_real_distribution;
94 result_type lo_, hi_, range_;
96 static_assert(std::is_floating_point<RealType>::value,
97 "Class-template absl::uniform_real_distribution<> must be "
98 "parameterized using a floating-point type.");
101 uniform_real_distribution() : uniform_real_distribution(0) {}
103 explicit uniform_real_distribution(result_type lo, result_type hi = 1)
106 explicit uniform_real_distribution(const param_type& param) : param_(param) {}
108 // uniform_real_distribution<T>::reset()
110 // Resets the uniform real distribution. Note that this function has no effect
111 // because the distribution already produces independent values.
114 template <typename URBG>
115 result_type operator()(URBG& gen) { // NOLINT(runtime/references)
116 return operator()(gen, param_);
119 template <typename URBG>
120 result_type operator()(URBG& gen, // NOLINT(runtime/references)
121 const param_type& p);
123 result_type a() const { return param_.a(); }
124 result_type b() const { return param_.b(); }
126 param_type param() const { return param_; }
127 void param(const param_type& params) { param_ = params; }
129 result_type(min)() const { return a(); }
130 result_type(max)() const { return b(); }
132 friend bool operator==(const uniform_real_distribution& a,
133 const uniform_real_distribution& b) {
134 return a.param_ == b.param_;
136 friend bool operator!=(const uniform_real_distribution& a,
137 const uniform_real_distribution& b) {
138 return a.param_ != b.param_;
143 random_internal::FastUniformBits<uint64_t> fast_u64_;
146 // -----------------------------------------------------------------------------
147 // Implementation details follow
148 // -----------------------------------------------------------------------------
149 template <typename RealType>
150 template <typename URBG>
151 typename uniform_real_distribution<RealType>::result_type
152 uniform_real_distribution<RealType>::operator()(
153 URBG& gen, const param_type& p) { // NOLINT(runtime/references)
154 using random_internal::PositiveValueT;
156 const result_type sample = random_internal::RandU64ToReal<
157 result_type>::template Value<PositiveValueT, true>(fast_u64_(gen));
158 const result_type res = p.a() + (sample * p.range_);
159 if (res < p.b() || p.range_ <= 0 || !std::isfinite(p.range_)) {
162 // else sample rejected, try again.
166 template <typename CharT, typename Traits, typename RealType>
167 std::basic_ostream<CharT, Traits>& operator<<(
168 std::basic_ostream<CharT, Traits>& os, // NOLINT(runtime/references)
169 const uniform_real_distribution<RealType>& x) {
170 auto saver = random_internal::make_ostream_state_saver(os);
171 os.precision(random_internal::stream_precision_helper<RealType>::kPrecision);
172 os << x.a() << os.fill() << x.b();
176 template <typename CharT, typename Traits, typename RealType>
177 std::basic_istream<CharT, Traits>& operator>>(
178 std::basic_istream<CharT, Traits>& is, // NOLINT(runtime/references)
179 uniform_real_distribution<RealType>& x) { // NOLINT(runtime/references)
180 using param_type = typename uniform_real_distribution<RealType>::param_type;
181 using result_type = typename uniform_real_distribution<RealType>::result_type;
182 auto saver = random_internal::make_istream_state_saver(is);
183 auto a = random_internal::read_floating_point<result_type>(is);
184 if (is.fail()) return is;
185 auto b = random_internal::read_floating_point<result_type>(is);
187 x.param(param_type(a, b));
193 #endif // ABSL_RANDOM_UNIFORM_REAL_DISTRIBUTION_H_