// Copyright 2017 The Abseil Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // ----------------------------------------------------------------------------- // File: uniform_real_distribution.h // ----------------------------------------------------------------------------- // // This header defines a class for representing a uniform floating-point // distribution over a half-open interval [a,b). You use this distribution in // combination with an Abseil random bit generator to produce random values // according to the rules of the distribution. // // `absl::uniform_real_distribution` is a drop-in replacement for the C++11 // `std::uniform_real_distribution` [rand.dist.uni.real] but is considerably // faster than the libstdc++ implementation. // // Note: the standard-library version may occasionally return `1.0` when // default-initialized. See https://bugs.llvm.org//show_bug.cgi?id=18767 // `absl::uniform_real_distribution` does not exhibit this behavior. #ifndef ABSL_RANDOM_UNIFORM_REAL_DISTRIBUTION_H_ #define ABSL_RANDOM_UNIFORM_REAL_DISTRIBUTION_H_ #include <cassert> #include <cmath> #include <cstdint> #include <istream> #include <limits> #include <type_traits> #include "absl/meta/type_traits.h" #include "absl/random/internal/fast_uniform_bits.h" #include "absl/random/internal/generate_real.h" #include "absl/random/internal/iostream_state_saver.h" namespace absl { ABSL_NAMESPACE_BEGIN // absl::uniform_real_distribution<T> // // This distribution produces random floating-point values uniformly distributed // over the half-open interval [a, b). // // Example: // // absl::BitGen gen; // // // Use the distribution to produce a value between 0.0 (inclusive) // // and 1.0 (exclusive). // double value = absl::uniform_real_distribution<double>(0, 1)(gen); // template <typename RealType = double> class uniform_real_distribution { … }; // ----------------------------------------------------------------------------- // Implementation details follow // ----------------------------------------------------------------------------- template <typename RealType> template <typename URBG> typename uniform_real_distribution<RealType>::result_type uniform_real_distribution<RealType>::operator()( URBG& gen, const param_type& p) { … } template <typename CharT, typename Traits, typename RealType> std::basic_ostream<CharT, Traits>& operator<<( std::basic_ostream<CharT, Traits>& os, // NOLINT(runtime/references) const uniform_real_distribution<RealType>& x) { … } template <typename CharT, typename Traits, typename RealType> std::basic_istream<CharT, Traits>& operator>>( std::basic_istream<CharT, Traits>& is, // NOLINT(runtime/references) uniform_real_distribution<RealType>& x) { … } ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_UNIFORM_REAL_DISTRIBUTION_H_