tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
uniform_real_distribution.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2023 Tobias Hienzsch
3
4#ifndef TETL_RANDOM_UNIFORM_REAL_DISTRIBUTION_HPP
5#define TETL_RANDOM_UNIFORM_REAL_DISTRIBUTION_HPP
6
7#include <etl/_limits/numeric_limits.hpp>
8#include <etl/_random/generate_canonical.hpp>
9
10namespace etl {
11
12/// \ingroup random
13template <typename RealType = double>
15 using result_type = RealType;
16
17 struct param_type {
18 using distribution_type = uniform_real_distribution;
19
20 constexpr param_type() noexcept = default;
21
22 explicit constexpr param_type(result_type min, result_type max = result_type(1)) noexcept
23 : _min{min}
24 , _max{max}
25 {
26 }
27
28 [[nodiscard]] constexpr auto a() const noexcept -> result_type
29 {
30 return _min;
31 }
32
33 [[nodiscard]] constexpr auto b() const noexcept -> result_type
34 {
35 return _max;
36 }
37
38 [[nodiscard]] friend constexpr auto operator==(param_type const& lhs, param_type const& rhs) noexcept -> bool
39 {
40 return (lhs._min == rhs._min) and (lhs._max == rhs._max);
41 }
42
43 private:
44 result_type _min{0};
45 result_type _max{1};
46 };
47
49 : uniform_real_distribution{static_cast<RealType>(0)}
50 {
51 }
52
53 explicit constexpr uniform_real_distribution(param_type const& parm)
54 : _param{parm}
55 {
56 }
57
58 explicit constexpr uniform_real_distribution(RealType a, RealType b = RealType(1))
60 {
61 }
62
63 constexpr auto param(param_type const& parm) -> void
64 {
65 _param = parm;
66 }
67
68 [[nodiscard]] constexpr auto param() const -> param_type
69 {
70 return _param;
71 }
72
73 [[nodiscard]] constexpr auto a() const -> result_type
74 {
75 return _param.a();
76 }
77
78 [[nodiscard]] constexpr auto b() const -> result_type
79 {
80 return _param.b();
81 }
82
83 [[nodiscard]] constexpr auto min() const -> result_type
84 {
85 return a();
86 }
87
88 [[nodiscard]] constexpr auto max() const -> result_type
89 {
90 return b();
91 }
92
93 constexpr auto reset() -> void
94 {
95 (void)this;
96 }
97
98 template <typename URBG>
99 [[nodiscard]] constexpr auto operator()(URBG& g) noexcept(noexcept(g())) -> result_type
100 {
101 return (*this)(g, _param);
102 }
103
104 template <typename URBG>
105 [[nodiscard]] constexpr auto operator()(URBG& g, param_type const& parm) noexcept(noexcept(g())) -> result_type
106 {
107 constexpr auto digits = static_cast<size_t>(numeric_limits<RealType>::digits);
108 constexpr auto bits = ~size_t{0};
109 constexpr auto minBits = digits < bits ? digits : bits;
110 static_assert(minBits <= 64);
111
112 // x = a + u * (b - a)
113 auto const a = parm.a();
114 auto const b = parm.b();
115 auto const u = etl::generate_canonical<RealType, minBits>(g);
116 return a + u * (b - a);
117 }
118
119 friend constexpr auto operator==(uniform_real_distribution const& x, uniform_real_distribution const& y) -> bool
120 {
121 return x.param() == y.param();
122 }
123
124private:
125 param_type _param;
126};
127
128} // namespace etl
129
130#endif // TETL_RANDOM_UNIFORM_REAL_DISTRIBUTION_HPP
Definition adjacent_find.hpp:9
Definition numeric_limits.hpp:18
Definition uniform_real_distribution.hpp:17
constexpr auto a() const noexcept -> result_type
Definition uniform_real_distribution.hpp:28
constexpr param_type() noexcept=default
constexpr param_type(result_type min, result_type max=result_type(1)) noexcept
Definition uniform_real_distribution.hpp:22
constexpr auto b() const noexcept -> result_type
Definition uniform_real_distribution.hpp:33
friend constexpr auto operator==(param_type const &lhs, param_type const &rhs) noexcept -> bool
Definition uniform_real_distribution.hpp:38
Definition uniform_real_distribution.hpp:14
constexpr uniform_real_distribution()
Definition uniform_real_distribution.hpp:48
constexpr auto param(param_type const &parm) -> void
Definition uniform_real_distribution.hpp:63
constexpr auto operator()(URBG &g) noexcept(noexcept(g())) -> result_type
Definition uniform_real_distribution.hpp:99
constexpr auto min() const -> result_type
Definition uniform_real_distribution.hpp:83
constexpr auto reset() -> void
Definition uniform_real_distribution.hpp:93
constexpr uniform_real_distribution(param_type const &parm)
Definition uniform_real_distribution.hpp:53
constexpr auto param() const -> param_type
Definition uniform_real_distribution.hpp:68
constexpr auto a() const -> result_type
Definition uniform_real_distribution.hpp:73
constexpr uniform_real_distribution(RealType a, RealType b=RealType(1))
Definition uniform_real_distribution.hpp:58
constexpr auto max() const -> result_type
Definition uniform_real_distribution.hpp:88
constexpr auto b() const -> result_type
Definition uniform_real_distribution.hpp:78
friend constexpr auto operator==(uniform_real_distribution const &x, uniform_real_distribution const &y) -> bool
Definition uniform_real_distribution.hpp:119
constexpr auto operator()(URBG &g, param_type const &parm) noexcept(noexcept(g())) -> result_type
Definition uniform_real_distribution.hpp:105