File indexing completed on 2026-05-03 08:14:01
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___RANDOM_UNIFORM_REAL_DISTRIBUTION_H
0010 #define _LIBCPP___RANDOM_UNIFORM_REAL_DISTRIBUTION_H
0011
0012 #include <__config>
0013 #include <__random/generate_canonical.h>
0014 #include <__random/is_valid.h>
0015 #include <iosfwd>
0016 #include <limits>
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 _RealType = double>
0028 class _LIBCPP_TEMPLATE_VIS uniform_real_distribution {
0029 static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
0030 "RealType must be a supported floating-point type");
0031
0032 public:
0033
0034 typedef _RealType result_type;
0035
0036 class _LIBCPP_TEMPLATE_VIS param_type {
0037 result_type __a_;
0038 result_type __b_;
0039
0040 public:
0041 typedef uniform_real_distribution distribution_type;
0042
0043 _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __a = 0, result_type __b = 1) : __a_(__a), __b_(__b) {}
0044
0045 _LIBCPP_HIDE_FROM_ABI result_type a() const { return __a_; }
0046 _LIBCPP_HIDE_FROM_ABI result_type b() const { return __b_; }
0047
0048 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
0049 return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;
0050 }
0051 friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
0052 };
0053
0054 private:
0055 param_type __p_;
0056
0057 public:
0058
0059 #ifndef _LIBCPP_CXX03_LANG
0060 _LIBCPP_HIDE_FROM_ABI uniform_real_distribution() : uniform_real_distribution(0) {}
0061 _LIBCPP_HIDE_FROM_ABI explicit uniform_real_distribution(result_type __a, result_type __b = 1)
0062 : __p_(param_type(__a, __b)) {}
0063 #else
0064 _LIBCPP_HIDE_FROM_ABI explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)
0065 : __p_(param_type(__a, __b)) {}
0066 #endif
0067 _LIBCPP_HIDE_FROM_ABI explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}
0068 _LIBCPP_HIDE_FROM_ABI void reset() {}
0069
0070
0071 template <class _URNG>
0072 _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
0073 return (*this)(__g, __p_);
0074 }
0075 template <class _URNG>
0076 _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
0077
0078
0079 _LIBCPP_HIDE_FROM_ABI result_type a() const { return __p_.a(); }
0080 _LIBCPP_HIDE_FROM_ABI result_type b() const { return __p_.b(); }
0081
0082 _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
0083 _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
0084
0085 _LIBCPP_HIDE_FROM_ABI result_type min() const { return a(); }
0086 _LIBCPP_HIDE_FROM_ABI result_type max() const { return b(); }
0087
0088 friend _LIBCPP_HIDE_FROM_ABI bool
0089 operator==(const uniform_real_distribution& __x, const uniform_real_distribution& __y) {
0090 return __x.__p_ == __y.__p_;
0091 }
0092 friend _LIBCPP_HIDE_FROM_ABI bool
0093 operator!=(const uniform_real_distribution& __x, const uniform_real_distribution& __y) {
0094 return !(__x == __y);
0095 }
0096 };
0097
0098 template <class _RealType>
0099 template <class _URNG>
0100 inline typename uniform_real_distribution<_RealType>::result_type
0101 uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) {
0102 static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
0103 return (__p.b() - __p.a()) * std::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g) + __p.a();
0104 }
0105
0106 template <class _CharT, class _Traits, class _RT>
0107 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0108 operator<<(basic_ostream<_CharT, _Traits>& __os, const uniform_real_distribution<_RT>& __x) {
0109 __save_flags<_CharT, _Traits> __lx(__os);
0110 typedef basic_ostream<_CharT, _Traits> _OStream;
0111 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
0112 _CharT __sp = __os.widen(' ');
0113 __os.fill(__sp);
0114 return __os << __x.a() << __sp << __x.b();
0115 }
0116
0117 template <class _CharT, class _Traits, class _RT>
0118 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
0119 operator>>(basic_istream<_CharT, _Traits>& __is, uniform_real_distribution<_RT>& __x) {
0120 typedef uniform_real_distribution<_RT> _Eng;
0121 typedef typename _Eng::result_type result_type;
0122 typedef typename _Eng::param_type param_type;
0123 __save_flags<_CharT, _Traits> __lx(__is);
0124 typedef basic_istream<_CharT, _Traits> _Istream;
0125 __is.flags(_Istream::dec | _Istream::skipws);
0126 result_type __a;
0127 result_type __b;
0128 __is >> __a >> __b;
0129 if (!__is.fail())
0130 __x.param(param_type(__a, __b));
0131 return __is;
0132 }
0133
0134 _LIBCPP_END_NAMESPACE_STD
0135
0136 _LIBCPP_POP_MACROS
0137
0138 #endif