// 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_ZIPF_DISTRIBUTION_H_ #define ABSL_RANDOM_ZIPF_DISTRIBUTION_H_ #include <cassert> #include <cmath> #include <istream> #include <limits> #include <ostream> #include <type_traits> #include "absl/random/internal/iostream_state_saver.h" #include "absl/random/internal/traits.h" #include "absl/random/uniform_real_distribution.h" namespace absl { ABSL_NAMESPACE_BEGIN // absl::zipf_distribution produces random integer-values in the range [0, k], // distributed according to the unnormalized discrete probability function: // // P(x) = (v + x) ^ -q // // The parameter `v` must be greater than 0 and the parameter `q` must be // greater than 1. If either of these parameters take invalid values then the // behavior is undefined. // // IntType is the result_type generated by the generator. It must be of integral // type; a static_assert ensures this is the case. // // The implementation is based on W.Hormann, G.Derflinger: // // "Rejection-Inversion to Generate Variates from Monotone Discrete // Distributions" // // http://eeyore.wu-wien.ac.at/papers/96-04-04.wh-der.ps.gz // template <typename IntType = int> class zipf_distribution { … }; // -------------------------------------------------------------------------- // Implementation details follow // -------------------------------------------------------------------------- template <typename IntType> zipf_distribution<IntType>::param_type::param_type( typename zipf_distribution<IntType>::result_type k, double q, double v) : … { … } template <typename IntType> double zipf_distribution<IntType>::param_type::h(double x) const { … } template <typename IntType> double zipf_distribution<IntType>::param_type::hinv(double x) const { … } template <typename IntType> double zipf_distribution<IntType>::param_type::compute_s() const { … } template <typename IntType> double zipf_distribution<IntType>::param_type::pow_negative_q(double x) const { … } template <typename IntType> template <typename URBG> typename zipf_distribution<IntType>::result_type zipf_distribution<IntType>::operator()( URBG& g, 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 zipf_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) zipf_distribution<IntType>& x) { … } ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_ZIPF_DISTRIBUTION_H_