// 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_int_distribution.h // ----------------------------------------------------------------------------- // // This header defines a class for representing a uniform integer distribution // over the closed (inclusive) 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_int_distribution` is a drop-in replacement for the C++11 // `std::uniform_int_distribution` [rand.dist.uni.int] but is considerably // faster than the libstdc++ implementation. #ifndef ABSL_RANDOM_UNIFORM_INT_DISTRIBUTION_H_ #define ABSL_RANDOM_UNIFORM_INT_DISTRIBUTION_H_ #include <cassert> #include <istream> #include <limits> #include <type_traits> #include "absl/base/optimization.h" #include "absl/random/internal/fast_uniform_bits.h" #include "absl/random/internal/iostream_state_saver.h" #include "absl/random/internal/traits.h" #include "absl/random/internal/wide_multiply.h" namespace absl { ABSL_NAMESPACE_BEGIN // absl::uniform_int_distribution<T> // // This distribution produces random integer values uniformly distributed in the // closed (inclusive) interval [a, b]. // // Example: // // absl::BitGen gen; // // // Use the distribution to produce a value between 1 and 6, inclusive. // int die_roll = absl::uniform_int_distribution<int>(1, 6)(gen); // template <typename IntType = int> class uniform_int_distribution { … }; // ----------------------------------------------------------------------------- // Implementation details follow // ----------------------------------------------------------------------------- template <typename CharT, typename Traits, typename IntType> std::basic_ostream<CharT, Traits>& operator<<( std::basic_ostream<CharT, Traits>& os, const uniform_int_distribution<IntType>& x) { … } template <typename CharT, typename Traits, typename IntType> std::basic_istream<CharT, Traits>& operator>>( std::basic_istream<CharT, Traits>& is, uniform_int_distribution<IntType>& x) { … } template <typename IntType> template <typename URBG> typename random_internal::make_unsigned_bits<IntType>::type uniform_int_distribution<IntType>::Generate( URBG& g, // NOLINT(runtime/references) typename random_internal::make_unsigned_bits<IntType>::type R) { … } ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_UNIFORM_INT_DISTRIBUTION_H_