File indexing completed on 2026-05-03 08:13:38
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___CXX03___RANDOM_BINOMIAL_DISTRIBUTION_H
0010 #define _LIBCPP___CXX03___RANDOM_BINOMIAL_DISTRIBUTION_H
0011
0012 #include <__cxx03/__config>
0013 #include <__cxx03/__random/is_valid.h>
0014 #include <__cxx03/__random/uniform_real_distribution.h>
0015 #include <__cxx03/cmath>
0016 #include <__cxx03/iosfwd>
0017
0018 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0019 # pragma GCC system_header
0020 #endif
0021
0022 _LIBCPP_PUSH_MACROS
0023 #include <__cxx03/__undef_macros>
0024
0025 _LIBCPP_BEGIN_NAMESPACE_STD
0026
0027 template <class _IntType = int>
0028 class _LIBCPP_TEMPLATE_VIS binomial_distribution {
0029 static_assert(__libcpp_random_is_valid_inttype<_IntType>::value, "IntType must be a supported integer type");
0030
0031 public:
0032
0033 typedef _IntType result_type;
0034
0035 class _LIBCPP_TEMPLATE_VIS param_type {
0036 result_type __t_;
0037 double __p_;
0038 double __pr_;
0039 double __odds_ratio_;
0040 result_type __r0_;
0041
0042 public:
0043 typedef binomial_distribution distribution_type;
0044
0045 _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __t = 1, double __p = 0.5);
0046
0047 _LIBCPP_HIDE_FROM_ABI result_type t() const { return __t_; }
0048 _LIBCPP_HIDE_FROM_ABI double p() const { return __p_; }
0049
0050 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
0051 return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;
0052 }
0053 friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
0054
0055 friend class binomial_distribution;
0056 };
0057
0058 private:
0059 param_type __p_;
0060
0061 public:
0062
0063 #ifndef _LIBCPP_CXX03_LANG
0064 _LIBCPP_HIDE_FROM_ABI binomial_distribution() : binomial_distribution(1) {}
0065 _LIBCPP_HIDE_FROM_ABI explicit binomial_distribution(result_type __t, double __p = 0.5)
0066 : __p_(param_type(__t, __p)) {}
0067 #else
0068 _LIBCPP_HIDE_FROM_ABI explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
0069 : __p_(param_type(__t, __p)) {}
0070 #endif
0071 _LIBCPP_HIDE_FROM_ABI explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
0072 _LIBCPP_HIDE_FROM_ABI void reset() {}
0073
0074
0075 template <class _URNG>
0076 _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
0077 return (*this)(__g, __p_);
0078 }
0079 template <class _URNG>
0080 _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
0081
0082
0083 _LIBCPP_HIDE_FROM_ABI result_type t() const { return __p_.t(); }
0084 _LIBCPP_HIDE_FROM_ABI double p() const { return __p_.p(); }
0085
0086 _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
0087 _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
0088
0089 _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }
0090 _LIBCPP_HIDE_FROM_ABI result_type max() const { return t(); }
0091
0092 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const binomial_distribution& __x, const binomial_distribution& __y) {
0093 return __x.__p_ == __y.__p_;
0094 }
0095 friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const binomial_distribution& __x, const binomial_distribution& __y) {
0096 return !(__x == __y);
0097 }
0098 };
0099
0100 #ifndef _LIBCPP_MSVCRT_LIKE
0101 extern "C" double lgamma_r(double, int*);
0102 #endif
0103
0104 inline _LIBCPP_HIDE_FROM_ABI double __libcpp_lgamma(double __d) {
0105 #if defined(_LIBCPP_MSVCRT_LIKE)
0106 return lgamma(__d);
0107 #else
0108 int __sign;
0109 return lgamma_r(__d, &__sign);
0110 #endif
0111 }
0112
0113 template <class _IntType>
0114 binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p) : __t_(__t), __p_(__p) {
0115 if (0 < __p_ && __p_ < 1) {
0116 __r0_ = static_cast<result_type>((__t_ + 1) * __p_);
0117 __pr_ = std::exp(
0118 std::__libcpp_lgamma(__t_ + 1.) - std::__libcpp_lgamma(__r0_ + 1.) - std::__libcpp_lgamma(__t_ - __r0_ + 1.) +
0119 __r0_ * std::log(__p_) + (__t_ - __r0_) * std::log(1 - __p_));
0120 __odds_ratio_ = __p_ / (1 - __p_);
0121 }
0122 }
0123
0124
0125
0126 template <class _IntType>
0127 template <class _URNG>
0128 _IntType binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr) {
0129 static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
0130 if (__pr.__t_ == 0 || __pr.__p_ == 0)
0131 return 0;
0132 if (__pr.__p_ == 1)
0133 return __pr.__t_;
0134 uniform_real_distribution<double> __gen;
0135 double __u = __gen(__g) - __pr.__pr_;
0136 if (__u < 0)
0137 return __pr.__r0_;
0138 double __pu = __pr.__pr_;
0139 double __pd = __pu;
0140 result_type __ru = __pr.__r0_;
0141 result_type __rd = __ru;
0142 while (true) {
0143 bool __break = true;
0144 if (__rd >= 1) {
0145 __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1));
0146 __u -= __pd;
0147 __break = false;
0148 if (__u < 0)
0149 return __rd - 1;
0150 }
0151 if (__rd != 0)
0152 --__rd;
0153 ++__ru;
0154 if (__ru <= __pr.__t_) {
0155 __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru;
0156 __u -= __pu;
0157 __break = false;
0158 if (__u < 0)
0159 return __ru;
0160 }
0161 if (__break)
0162 return 0;
0163 }
0164 }
0165
0166 template <class _CharT, class _Traits, class _IntType>
0167 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0168 operator<<(basic_ostream<_CharT, _Traits>& __os, const binomial_distribution<_IntType>& __x) {
0169 __save_flags<_CharT, _Traits> __lx(__os);
0170 typedef basic_ostream<_CharT, _Traits> _OStream;
0171 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
0172 _CharT __sp = __os.widen(' ');
0173 __os.fill(__sp);
0174 return __os << __x.t() << __sp << __x.p();
0175 }
0176
0177 template <class _CharT, class _Traits, class _IntType>
0178 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
0179 operator>>(basic_istream<_CharT, _Traits>& __is, binomial_distribution<_IntType>& __x) {
0180 typedef binomial_distribution<_IntType> _Eng;
0181 typedef typename _Eng::result_type result_type;
0182 typedef typename _Eng::param_type param_type;
0183 __save_flags<_CharT, _Traits> __lx(__is);
0184 typedef basic_istream<_CharT, _Traits> _Istream;
0185 __is.flags(_Istream::dec | _Istream::skipws);
0186 result_type __t;
0187 double __p;
0188 __is >> __t >> __p;
0189 if (!__is.fail())
0190 __x.param(param_type(__t, __p));
0191 return __is;
0192 }
0193
0194 _LIBCPP_END_NAMESPACE_STD
0195
0196 _LIBCPP_POP_MACROS
0197
0198 #endif