xref: /llvm-project/libcxx/include/__random/uniform_real_distribution.h (revision 9783f28cbb155e4a8d49c12e1c60ce14dcfaf0c7)
1344cef66SArthur O'Dwyer //===----------------------------------------------------------------------===//
2344cef66SArthur O'Dwyer //
3344cef66SArthur O'Dwyer // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4344cef66SArthur O'Dwyer // See https://llvm.org/LICENSE.txt for license information.
5344cef66SArthur O'Dwyer // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6344cef66SArthur O'Dwyer //
7344cef66SArthur O'Dwyer //===----------------------------------------------------------------------===//
8344cef66SArthur O'Dwyer 
9344cef66SArthur O'Dwyer #ifndef _LIBCPP___RANDOM_UNIFORM_REAL_DISTRIBUTION_H
10344cef66SArthur O'Dwyer #define _LIBCPP___RANDOM_UNIFORM_REAL_DISTRIBUTION_H
11344cef66SArthur O'Dwyer 
12344cef66SArthur O'Dwyer #include <__config>
13344cef66SArthur O'Dwyer #include <__random/generate_canonical.h>
147624552eSArthur O'Dwyer #include <__random/is_valid.h>
15344cef66SArthur O'Dwyer #include <iosfwd>
16344cef66SArthur O'Dwyer #include <limits>
17344cef66SArthur O'Dwyer 
18344cef66SArthur O'Dwyer #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19344cef66SArthur O'Dwyer #  pragma GCC system_header
20344cef66SArthur O'Dwyer #endif
21344cef66SArthur O'Dwyer 
22344cef66SArthur O'Dwyer _LIBCPP_PUSH_MACROS
23344cef66SArthur O'Dwyer #include <__undef_macros>
24344cef66SArthur O'Dwyer 
25344cef66SArthur O'Dwyer _LIBCPP_BEGIN_NAMESPACE_STD
26344cef66SArthur O'Dwyer 
27344cef66SArthur O'Dwyer template <class _RealType = double>
28*9783f28cSLouis Dionne class _LIBCPP_TEMPLATE_VIS uniform_real_distribution {
29727fef79SNhat Nguyen   static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
30727fef79SNhat Nguyen                 "RealType must be a supported floating-point type");
31727fef79SNhat Nguyen 
32344cef66SArthur O'Dwyer public:
33344cef66SArthur O'Dwyer   // types
34344cef66SArthur O'Dwyer   typedef _RealType result_type;
35344cef66SArthur O'Dwyer 
36*9783f28cSLouis Dionne   class _LIBCPP_TEMPLATE_VIS param_type {
37344cef66SArthur O'Dwyer     result_type __a_;
38344cef66SArthur O'Dwyer     result_type __b_;
39*9783f28cSLouis Dionne 
40344cef66SArthur O'Dwyer   public:
41344cef66SArthur O'Dwyer     typedef uniform_real_distribution distribution_type;
42344cef66SArthur O'Dwyer 
__a_(__a)43*9783f28cSLouis Dionne     _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __a = 0, result_type __b = 1) : __a_(__a), __b_(__b) {}
44344cef66SArthur O'Dwyer 
a()45*9783f28cSLouis Dionne     _LIBCPP_HIDE_FROM_ABI result_type a() const { return __a_; }
b()46*9783f28cSLouis Dionne     _LIBCPP_HIDE_FROM_ABI result_type b() const { return __b_; }
47344cef66SArthur O'Dwyer 
48*9783f28cSLouis Dionne     friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
49*9783f28cSLouis Dionne       return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;
50*9783f28cSLouis Dionne     }
51*9783f28cSLouis Dionne     friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
52344cef66SArthur O'Dwyer   };
53344cef66SArthur O'Dwyer 
54344cef66SArthur O'Dwyer private:
55344cef66SArthur O'Dwyer   param_type __p_;
56344cef66SArthur O'Dwyer 
57344cef66SArthur O'Dwyer public:
58344cef66SArthur O'Dwyer   // constructors and reset functions
59344cef66SArthur O'Dwyer #ifndef _LIBCPP_CXX03_LANG
uniform_real_distribution()60*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI uniform_real_distribution() : uniform_real_distribution(0) {}
6183ce1397SNikolas Klauser   _LIBCPP_HIDE_FROM_ABI explicit uniform_real_distribution(result_type __a, result_type __b = 1)
__p_(param_type (__a,__b))62344cef66SArthur O'Dwyer       : __p_(param_type(__a, __b)) {}
63344cef66SArthur O'Dwyer #else
64*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI explicit uniform_real_distribution(result_type __a = 0, result_type __b = 1)
65344cef66SArthur O'Dwyer       : __p_(param_type(__a, __b)) {}
66344cef66SArthur O'Dwyer #endif
uniform_real_distribution(const param_type & __p)67*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI explicit uniform_real_distribution(const param_type& __p) : __p_(__p) {}
reset()68*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI void reset() {}
69344cef66SArthur O'Dwyer 
70344cef66SArthur O'Dwyer   // generating functions
71344cef66SArthur O'Dwyer   template <class _URNG>
operator()72*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
73*9783f28cSLouis Dionne     return (*this)(__g, __p_);
74*9783f28cSLouis Dionne   }
75*9783f28cSLouis Dionne   template <class _URNG>
76*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
77344cef66SArthur O'Dwyer 
78344cef66SArthur O'Dwyer   // property functions
a()79*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI result_type a() const { return __p_.a(); }
b()80*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI result_type b() const { return __p_.b(); }
81344cef66SArthur O'Dwyer 
param()82*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
param(const param_type & __p)83*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
84344cef66SArthur O'Dwyer 
min()85*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI result_type min() const { return a(); }
max()86*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI result_type max() const { return b(); }
87344cef66SArthur O'Dwyer 
88*9783f28cSLouis Dionne   friend _LIBCPP_HIDE_FROM_ABI bool
89*9783f28cSLouis Dionne   operator==(const uniform_real_distribution& __x, const uniform_real_distribution& __y) {
90*9783f28cSLouis Dionne     return __x.__p_ == __y.__p_;
91*9783f28cSLouis Dionne   }
92*9783f28cSLouis Dionne   friend _LIBCPP_HIDE_FROM_ABI bool
93*9783f28cSLouis Dionne   operator!=(const uniform_real_distribution& __x, const uniform_real_distribution& __y) {
94*9783f28cSLouis Dionne     return !(__x == __y);
95*9783f28cSLouis Dionne   }
96344cef66SArthur O'Dwyer };
97344cef66SArthur O'Dwyer 
98344cef66SArthur O'Dwyer template <class _RealType>
99344cef66SArthur O'Dwyer template <class _URNG>
100*9783f28cSLouis Dionne inline typename uniform_real_distribution<_RealType>::result_type
operator()101*9783f28cSLouis Dionne uniform_real_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) {
1027624552eSArthur O'Dwyer   static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
103*9783f28cSLouis Dionne   return (__p.b() - __p.a()) * std::generate_canonical<_RealType, numeric_limits<_RealType>::digits>(__g) + __p.a();
104344cef66SArthur O'Dwyer }
105344cef66SArthur O'Dwyer 
106344cef66SArthur O'Dwyer template <class _CharT, class _Traits, class _RT>
10780c7e93aSNikolas Klauser _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
108*9783f28cSLouis Dionne operator<<(basic_ostream<_CharT, _Traits>& __os, const uniform_real_distribution<_RT>& __x) {
109344cef66SArthur O'Dwyer   __save_flags<_CharT, _Traits> __lx(__os);
110344cef66SArthur O'Dwyer   typedef basic_ostream<_CharT, _Traits> _OStream;
111*9783f28cSLouis Dionne   __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
112344cef66SArthur O'Dwyer   _CharT __sp = __os.widen(' ');
113344cef66SArthur O'Dwyer   __os.fill(__sp);
114344cef66SArthur O'Dwyer   return __os << __x.a() << __sp << __x.b();
115344cef66SArthur O'Dwyer }
116344cef66SArthur O'Dwyer 
117344cef66SArthur O'Dwyer template <class _CharT, class _Traits, class _RT>
11880c7e93aSNikolas Klauser _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
119*9783f28cSLouis Dionne operator>>(basic_istream<_CharT, _Traits>& __is, uniform_real_distribution<_RT>& __x) {
120344cef66SArthur O'Dwyer   typedef uniform_real_distribution<_RT> _Eng;
121344cef66SArthur O'Dwyer   typedef typename _Eng::result_type result_type;
122344cef66SArthur O'Dwyer   typedef typename _Eng::param_type param_type;
123344cef66SArthur O'Dwyer   __save_flags<_CharT, _Traits> __lx(__is);
124344cef66SArthur O'Dwyer   typedef basic_istream<_CharT, _Traits> _Istream;
125344cef66SArthur O'Dwyer   __is.flags(_Istream::dec | _Istream::skipws);
126344cef66SArthur O'Dwyer   result_type __a;
127344cef66SArthur O'Dwyer   result_type __b;
128344cef66SArthur O'Dwyer   __is >> __a >> __b;
129344cef66SArthur O'Dwyer   if (!__is.fail())
130344cef66SArthur O'Dwyer     __x.param(param_type(__a, __b));
131344cef66SArthur O'Dwyer   return __is;
132344cef66SArthur O'Dwyer }
133344cef66SArthur O'Dwyer 
134344cef66SArthur O'Dwyer _LIBCPP_END_NAMESPACE_STD
135344cef66SArthur O'Dwyer 
136344cef66SArthur O'Dwyer _LIBCPP_POP_MACROS
137344cef66SArthur O'Dwyer 
138344cef66SArthur O'Dwyer #endif // _LIBCPP___RANDOM_UNIFORM_REAL_DISTRIBUTION_H
139