// 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. #ifndef ABSL_RANDOM_GAUSSIAN_DISTRIBUTION_H_ #define ABSL_RANDOM_GAUSSIAN_DISTRIBUTION_H_ // absl::gaussian_distribution implements the Ziggurat algorithm // for generating random gaussian numbers. // // Implementation based on "The Ziggurat Method for Generating Random Variables" // by George Marsaglia and Wai Wan Tsang: http://www.jstatsoft.org/v05/i08/ // #include <cmath> #include <cstdint> #include <istream> #include <limits> #include <type_traits> #include "absl/base/config.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 namespace random_internal { // absl::gaussian_distribution_base implements the underlying ziggurat algorithm // using the ziggurat tables generated by the gaussian_distribution_gentables // binary. // // The specific algorithm has some of the improvements suggested by the // 2005 paper, "An Improved Ziggurat Method to Generate Normal Random Samples", // Jurgen A Doornik. (https://www.doornik.com/research/ziggurat.pdf) class ABSL_DLL gaussian_distribution_base { … }; } // namespace random_internal // absl::gaussian_distribution: // Generates a number conforming to a Gaussian distribution. template <typename RealType = double> class gaussian_distribution : random_internal::gaussian_distribution_base { … }; // -------------------------------------------------------------------------- // Implementation details only below // -------------------------------------------------------------------------- template <typename RealType> template <typename URBG> typename gaussian_distribution<RealType>::result_type gaussian_distribution<RealType>::operator()( URBG& g, // NOLINT(runtime/references) 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 gaussian_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) gaussian_distribution<RealType>& x) { … } namespace random_internal { template <typename URBG> inline double gaussian_distribution_base::zignor_fallback(URBG& g, bool neg) { … } template <typename URBG> inline double gaussian_distribution_base::zignor( URBG& g) { … } } // namespace random_internal ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_GAUSSIAN_DISTRIBUTION_H_