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 #ifndef ABSL_RANDOM_EXPONENTIAL_DISTRIBUTION_H_
16 #define ABSL_RANDOM_EXPONENTIAL_DISTRIBUTION_H_
22 #include <type_traits>
24 #include "absl/random/internal/distribution_impl.h"
25 #include "absl/random/internal/fast_uniform_bits.h"
26 #include "absl/random/internal/iostream_state_saver.h"
30 // absl::exponential_distribution:
31 // Generates a number conforming to an exponential distribution and is
32 // equivalent to the standard [rand.dist.pois.exp] distribution.
33 template <typename RealType = double>
34 class exponential_distribution {
36 using result_type = RealType;
40 using distribution_type = exponential_distribution;
42 explicit param_type(result_type lambda = 1) : lambda_(lambda) {
44 neg_inv_lambda_ = -result_type(1) / lambda_;
47 result_type lambda() const { return lambda_; }
49 friend bool operator==(const param_type& a, const param_type& b) {
50 return a.lambda_ == b.lambda_;
53 friend bool operator!=(const param_type& a, const param_type& b) {
58 friend class exponential_distribution;
61 result_type neg_inv_lambda_;
64 std::is_floating_point<RealType>::value,
65 "Class-template absl::exponential_distribution<> must be parameterized "
66 "using a floating-point type.");
69 exponential_distribution() : exponential_distribution(1) {}
71 explicit exponential_distribution(result_type lambda) : param_(lambda) {}
73 explicit exponential_distribution(const param_type& p) : param_(p) {}
77 // Generating functions
78 template <typename URBG>
79 result_type operator()(URBG& g) { // NOLINT(runtime/references)
80 return (*this)(g, param_);
83 template <typename URBG>
84 result_type operator()(URBG& g, // NOLINT(runtime/references)
87 param_type param() const { return param_; }
88 void param(const param_type& p) { param_ = p; }
90 result_type(min)() const { return 0; }
91 result_type(max)() const {
92 return std::numeric_limits<result_type>::infinity();
95 result_type lambda() const { return param_.lambda(); }
97 friend bool operator==(const exponential_distribution& a,
98 const exponential_distribution& b) {
99 return a.param_ == b.param_;
101 friend bool operator!=(const exponential_distribution& a,
102 const exponential_distribution& b) {
103 return a.param_ != b.param_;
108 random_internal::FastUniformBits<uint64_t> fast_u64_;
111 // --------------------------------------------------------------------------
112 // Implementation details follow
113 // --------------------------------------------------------------------------
115 template <typename RealType>
116 template <typename URBG>
117 typename exponential_distribution<RealType>::result_type
118 exponential_distribution<RealType>::operator()(
119 URBG& g, // NOLINT(runtime/references)
120 const param_type& p) {
121 using random_internal::NegativeValueT;
122 const result_type u = random_internal::RandU64ToReal<
123 result_type>::template Value<NegativeValueT, false>(fast_u64_(g));
124 // log1p(-x) is mathematically equivalent to log(1 - x) but has more
125 // accuracy for x near zero.
126 return p.neg_inv_lambda_ * std::log1p(u);
129 template <typename CharT, typename Traits, typename RealType>
130 std::basic_ostream<CharT, Traits>& operator<<(
131 std::basic_ostream<CharT, Traits>& os, // NOLINT(runtime/references)
132 const exponential_distribution<RealType>& x) {
133 auto saver = random_internal::make_ostream_state_saver(os);
134 os.precision(random_internal::stream_precision_helper<RealType>::kPrecision);
139 template <typename CharT, typename Traits, typename RealType>
140 std::basic_istream<CharT, Traits>& operator>>(
141 std::basic_istream<CharT, Traits>& is, // NOLINT(runtime/references)
142 exponential_distribution<RealType>& x) { // NOLINT(runtime/references)
143 using result_type = typename exponential_distribution<RealType>::result_type;
144 using param_type = typename exponential_distribution<RealType>::param_type;
147 auto saver = random_internal::make_istream_state_saver(is);
148 lambda = random_internal::read_floating_point<result_type>(is);
150 x.param(param_type(lambda));
157 #endif // ABSL_RANDOM_EXPONENTIAL_DISTRIBUTION_H_