File indexing completed on 2026-05-03 08:13:59
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___RANDOM_BINOMIAL_DISTRIBUTION_H
0010 #define _LIBCPP___RANDOM_BINOMIAL_DISTRIBUTION_H
0011
0012 #include <__config>
0013 #include <__random/is_valid.h>
0014 #include <__random/uniform_real_distribution.h>
0015 #include <cmath>
0016 #include <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 <__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
0101 #if !defined(_LIBCPP_MSVCRT_LIKE) && !defined(__LLVM_LIBC__)
0102 extern "C" double lgamma_r(double, int*);
0103 #endif
0104
0105 inline _LIBCPP_HIDE_FROM_ABI double __libcpp_lgamma(double __d) {
0106 #if defined(_LIBCPP_MSVCRT_LIKE) || defined(__LLVM_LIBC__)
0107 return lgamma(__d);
0108 #else
0109 int __sign;
0110 return lgamma_r(__d, &__sign);
0111 #endif
0112 }
0113
0114 template <class _IntType>
0115 binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p) : __t_(__t), __p_(__p) {
0116 if (0 < __p_ && __p_ < 1) {
0117 __r0_ = static_cast<result_type>((__t_ + 1) * __p_);
0118 __pr_ = std::exp(
0119 std::__libcpp_lgamma(__t_ + 1.) - std::__libcpp_lgamma(__r0_ + 1.) - std::__libcpp_lgamma(__t_ - __r0_ + 1.) +
0120 __r0_ * std::log(__p_) + (__t_ - __r0_) * std::log(1 - __p_));
0121 __odds_ratio_ = __p_ / (1 - __p_);
0122 }
0123 }
0124
0125
0126
0127 template <class _IntType>
0128 template <class _URNG>
0129 _IntType binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr) {
0130 static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
0131 if (__pr.__t_ == 0 || __pr.__p_ == 0)
0132 return 0;
0133 if (__pr.__p_ == 1)
0134 return __pr.__t_;
0135 uniform_real_distribution<double> __gen;
0136 double __u = __gen(__g) - __pr.__pr_;
0137 if (__u < 0)
0138 return __pr.__r0_;
0139 double __pu = __pr.__pr_;
0140 double __pd = __pu;
0141 result_type __ru = __pr.__r0_;
0142 result_type __rd = __ru;
0143 while (true) {
0144 bool __break = true;
0145 if (__rd >= 1) {
0146 __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1));
0147 __u -= __pd;
0148 __break = false;
0149 if (__u < 0)
0150 return __rd - 1;
0151 }
0152 if (__rd != 0)
0153 --__rd;
0154 ++__ru;
0155 if (__ru <= __pr.__t_) {
0156 __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru;
0157 __u -= __pu;
0158 __break = false;
0159 if (__u < 0)
0160 return __ru;
0161 }
0162 if (__break)
0163 return 0;
0164 }
0165 }
0166
0167 template <class _CharT, class _Traits, class _IntType>
0168 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0169 operator<<(basic_ostream<_CharT, _Traits>& __os, const binomial_distribution<_IntType>& __x) {
0170 __save_flags<_CharT, _Traits> __lx(__os);
0171 typedef basic_ostream<_CharT, _Traits> _OStream;
0172 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
0173 _CharT __sp = __os.widen(' ');
0174 __os.fill(__sp);
0175 return __os << __x.t() << __sp << __x.p();
0176 }
0177
0178 template <class _CharT, class _Traits, class _IntType>
0179 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
0180 operator>>(basic_istream<_CharT, _Traits>& __is, binomial_distribution<_IntType>& __x) {
0181 typedef binomial_distribution<_IntType> _Eng;
0182 typedef typename _Eng::result_type result_type;
0183 typedef typename _Eng::param_type param_type;
0184 __save_flags<_CharT, _Traits> __lx(__is);
0185 typedef basic_istream<_CharT, _Traits> _Istream;
0186 __is.flags(_Istream::dec | _Istream::skipws);
0187 result_type __t;
0188 double __p;
0189 __is >> __t >> __p;
0190 if (!__is.fail())
0191 __x.param(param_type(__t, __p));
0192 return __is;
0193 }
0194
0195 _LIBCPP_END_NAMESPACE_STD
0196
0197 _LIBCPP_POP_MACROS
0198
0199 #endif