1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___RANDOM_UNIFORM_REAL_DISTRIBUTION_H
10 #define _LIBCPP___RANDOM_UNIFORM_REAL_DISTRIBUTION_H
13 #include <__random/generate_canonical.h>
14 #include <__random/is_valid.h>
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 # pragma GCC system_header
23 #include <__undef_macros>
25 _LIBCPP_BEGIN_NAMESPACE_STD
27 template<class _RealType
= double>
28 class _LIBCPP_TEMPLATE_VIS uniform_real_distribution
32 typedef _RealType result_type
;
34 class _LIBCPP_TEMPLATE_VIS param_type
39 typedef uniform_real_distribution distribution_type
;
41 _LIBCPP_INLINE_VISIBILITY
42 explicit param_type(result_type __a
= 0,
44 : __a_(__a
), __b_(__b
) {}
46 _LIBCPP_INLINE_VISIBILITY
47 result_type
a() const {return __a_
;}
48 _LIBCPP_INLINE_VISIBILITY
49 result_type
b() const {return __b_
;}
51 friend _LIBCPP_INLINE_VISIBILITY
52 bool operator==(const param_type
& __x
, const param_type
& __y
)
53 {return __x
.__a_
== __y
.__a_
&& __x
.__b_
== __y
.__b_
;}
54 friend _LIBCPP_INLINE_VISIBILITY
55 bool operator!=(const param_type
& __x
, const param_type
& __y
)
56 {return !(__x
== __y
);}
63 // constructors and reset functions
64 #ifndef _LIBCPP_CXX03_LANG
65 _LIBCPP_INLINE_VISIBILITY
66 uniform_real_distribution() : uniform_real_distribution(0) {}
67 _LIBCPP_HIDE_FROM_ABI
explicit uniform_real_distribution(result_type __a
, result_type __b
= 1)
68 : __p_(param_type(__a
, __b
)) {}
70 _LIBCPP_INLINE_VISIBILITY
71 explicit uniform_real_distribution(result_type __a
= 0, result_type __b
= 1)
72 : __p_(param_type(__a
, __b
)) {}
74 _LIBCPP_INLINE_VISIBILITY
75 explicit uniform_real_distribution(const param_type
& __p
) : __p_(__p
) {}
76 _LIBCPP_INLINE_VISIBILITY
79 // generating functions
81 _LIBCPP_INLINE_VISIBILITY
82 result_type
operator()(_URNG
& __g
)
83 {return (*this)(__g
, __p_
);}
84 template<class _URNG
> _LIBCPP_INLINE_VISIBILITY result_type
operator()(_URNG
& __g
, const param_type
& __p
);
87 _LIBCPP_INLINE_VISIBILITY
88 result_type
a() const {return __p_
.a();}
89 _LIBCPP_INLINE_VISIBILITY
90 result_type
b() const {return __p_
.b();}
92 _LIBCPP_INLINE_VISIBILITY
93 param_type
param() const {return __p_
;}
94 _LIBCPP_INLINE_VISIBILITY
95 void param(const param_type
& __p
) {__p_
= __p
;}
97 _LIBCPP_INLINE_VISIBILITY
98 result_type
min() const {return a();}
99 _LIBCPP_INLINE_VISIBILITY
100 result_type
max() const {return b();}
102 friend _LIBCPP_INLINE_VISIBILITY
103 bool operator==(const uniform_real_distribution
& __x
,
104 const uniform_real_distribution
& __y
)
105 {return __x
.__p_
== __y
.__p_
;}
106 friend _LIBCPP_INLINE_VISIBILITY
107 bool operator!=(const uniform_real_distribution
& __x
,
108 const uniform_real_distribution
& __y
)
109 {return !(__x
== __y
);}
112 template<class _RealType
>
113 template<class _URNG
>
115 typename uniform_real_distribution
<_RealType
>::result_type
116 uniform_real_distribution
<_RealType
>::operator()(_URNG
& __g
, const param_type
& __p
)
118 static_assert(__libcpp_random_is_valid_urng
<_URNG
>::value
, "");
119 return (__p
.b() - __p
.a())
120 * _VSTD::generate_canonical
<_RealType
, numeric_limits
<_RealType
>::digits
>(__g
)
124 template <class _CharT
, class _Traits
, class _RT
>
125 _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>&
126 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
,
127 const uniform_real_distribution
<_RT
>& __x
)
129 __save_flags
<_CharT
, _Traits
> __lx(__os
);
130 typedef basic_ostream
<_CharT
, _Traits
> _OStream
;
131 __os
.flags(_OStream::dec
| _OStream::left
| _OStream::fixed
|
132 _OStream::scientific
);
133 _CharT __sp
= __os
.widen(' ');
135 return __os
<< __x
.a() << __sp
<< __x
.b();
138 template <class _CharT
, class _Traits
, class _RT
>
139 _LIBCPP_HIDE_FROM_ABI basic_istream
<_CharT
, _Traits
>&
140 operator>>(basic_istream
<_CharT
, _Traits
>& __is
,
141 uniform_real_distribution
<_RT
>& __x
)
143 typedef uniform_real_distribution
<_RT
> _Eng
;
144 typedef typename
_Eng::result_type result_type
;
145 typedef typename
_Eng::param_type param_type
;
146 __save_flags
<_CharT
, _Traits
> __lx(__is
);
147 typedef basic_istream
<_CharT
, _Traits
> _Istream
;
148 __is
.flags(_Istream::dec
| _Istream::skipws
);
153 __x
.param(param_type(__a
, __b
));
157 _LIBCPP_END_NAMESPACE_STD
161 #endif // _LIBCPP___RANDOM_UNIFORM_REAL_DISTRIBUTION_H