// 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_POISSON_DISTRIBUTION_H_ #define ABSL_RANDOM_POISSON_DISTRIBUTION_H_ #include <cassert> #include <cmath> #include <istream> #include <limits> #include <ostream> #include <type_traits> #include "absl/random/internal/fast_uniform_bits.h" #include "absl/random/internal/fastmath.h" #include "absl/random/internal/generate_real.h" #include "absl/random/internal/iostream_state_saver.h" #include "absl/random/internal/traits.h" namespace absl { ABSL_NAMESPACE_BEGIN // absl::poisson_distribution: // Generates discrete variates conforming to a Poisson distribution. // p(n) = (mean^n / n!) exp(-mean) // // Depending on the parameter, the distribution selects one of the following // algorithms: // * The standard algorithm, attributed to Knuth, extended using a split method // for larger values // * The "Ratio of Uniforms as a convenient method for sampling from classical // discrete distributions", Stadlober, 1989. // http://www.sciencedirect.com/science/article/pii/0377042790903495 // // NOTE: param_type.mean() is a double, which permits values larger than // poisson_distribution<IntType>::max(), however this should be avoided and // the distribution results are limited to the max() value. // // The goals of this implementation are to provide good performance while still // beig thread-safe: This limits the implementation to not using lgamma provided // by <math.h>. // template <typename IntType = int> class poisson_distribution { … }; // ----------------------------------------------------------------------------- // Implementation details follow // ----------------------------------------------------------------------------- template <typename IntType> poisson_distribution<IntType>::param_type::param_type(double mean) : … { … } template <typename IntType> template <typename URBG> typename poisson_distribution<IntType>::result_type poisson_distribution<IntType>::operator()( URBG& g, // NOLINT(runtime/references) const param_type& p) { … } template <typename CharT, typename Traits, typename IntType> std::basic_ostream<CharT, Traits>& operator<<( std::basic_ostream<CharT, Traits>& os, // NOLINT(runtime/references) const poisson_distribution<IntType>& x) { … } template <typename CharT, typename Traits, typename IntType> std::basic_istream<CharT, Traits>& operator>>( std::basic_istream<CharT, Traits>& is, // NOLINT(runtime/references) poisson_distribution<IntType>& x) { … } ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_POISSON_DISTRIBUTION_H_