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
3#ifndef TETL_RANDOM_UNIFORM_REAL_DISTRIBUTION_HPP
4#define TETL_RANDOM_UNIFORM_REAL_DISTRIBUTION_HPP
5
8
9namespace etl {
10
12template <typename RealType = double>
14 using result_type = RealType;
15
16 struct param_type {
18
19 constexpr param_type() noexcept = default;
20
21 explicit constexpr param_type(result_type min, result_type max = result_type(1)) noexcept
22 : _min{min}
23 , _max{max}
24 {
25 }
26
27 [[nodiscard]] constexpr auto a() const noexcept -> result_type { return _min; }
28
29 [[nodiscard]] constexpr auto b() const noexcept -> result_type { return _max; }
30
31 [[nodiscard]] friend constexpr auto operator==(param_type const& lhs, param_type const& rhs) noexcept -> bool
32 {
33 return (lhs._min == rhs._min) and (lhs._max == rhs._max);
34 }
35
36 private:
37 result_type _min{0};
38 result_type _max{1};
39 };
40
42 : uniform_real_distribution{static_cast<RealType>(0)}
43 {
44 }
45
46 explicit constexpr uniform_real_distribution(param_type const& parm)
47 : _param{parm}
48 {
49 }
50
51 explicit constexpr uniform_real_distribution(RealType a, RealType b = RealType(1))
53 {
54 }
55
56 constexpr auto param(param_type const& parm) -> void { _param = parm; }
57
58 [[nodiscard]] constexpr auto param() const -> param_type { return _param; }
59
60 [[nodiscard]] constexpr auto a() const -> result_type { return _param.a(); }
61
62 [[nodiscard]] constexpr auto b() const -> result_type { return _param.b(); }
63
64 [[nodiscard]] constexpr auto min() const -> result_type { return a(); }
65
66 [[nodiscard]] constexpr auto max() const -> result_type { return b(); }
67
68 constexpr auto reset() -> void { (void)this; }
69
70 template <typename URBG>
71 [[nodiscard]] constexpr auto operator()(URBG& g) noexcept(noexcept(g())) -> result_type
72 {
73 return (*this)(g, _param);
74 }
75
76 template <typename URBG>
77 [[nodiscard]] constexpr auto operator()(URBG& g, param_type const& parm) noexcept(noexcept(g())) -> result_type
78 {
79 constexpr auto digits = static_cast<size_t>(numeric_limits<RealType>::digits);
80 constexpr auto bits = ~size_t{0};
81 constexpr auto minBits = digits < bits ? digits : bits;
82 static_assert(minBits <= 64);
83
84 // x = a + u * (b - a)
85 auto const a = parm.a();
86 auto const b = parm.b();
88 return a + u * (b - a);
89 }
90
91 friend constexpr auto operator==(uniform_real_distribution const& x, uniform_real_distribution const& y) -> bool
92 {
93 return x.param() == y.param();
94 }
95
96private:
97 param_type _param;
98};
99
100} // namespace etl
101
102#endif // TETL_RANDOM_UNIFORM_REAL_DISTRIBUTION_HPP
constexpr auto generate_canonical(RNG &g) noexcept(noexcept(g())) -> Real
Generates a random floating point number in range [0,1).
Definition generate_canonical.hpp:44
Definition adjacent_find.hpp:8
TETL_BUILTIN_SIZET size_t
etl::size_t is the unsigned integer type of the result of the sizeof operator.
Definition size_t.hpp:14
static constexpr int digits
Definition numeric_limits.hpp:24
Definition uniform_real_distribution.hpp:16
uniform_real_distribution distribution_type
Definition uniform_real_distribution.hpp:17
constexpr auto a() const noexcept -> result_type
Definition uniform_real_distribution.hpp:27
constexpr param_type() noexcept=default
constexpr auto b() const noexcept -> result_type
Definition uniform_real_distribution.hpp:29
friend constexpr auto operator==(param_type const &lhs, param_type const &rhs) noexcept -> bool
Definition uniform_real_distribution.hpp:31
constexpr uniform_real_distribution()
Definition uniform_real_distribution.hpp:41
constexpr auto param(param_type const &parm) -> void
Definition uniform_real_distribution.hpp:56
constexpr auto operator()(URBG &g) noexcept(noexcept(g())) -> result_type
Definition uniform_real_distribution.hpp:71
constexpr auto min() const -> result_type
Definition uniform_real_distribution.hpp:64
constexpr auto reset() -> void
Definition uniform_real_distribution.hpp:68
constexpr uniform_real_distribution(param_type const &parm)
Definition uniform_real_distribution.hpp:46
constexpr auto param() const -> param_type
Definition uniform_real_distribution.hpp:58
constexpr auto a() const -> result_type
Definition uniform_real_distribution.hpp:60
constexpr uniform_real_distribution(RealType a, RealType b=RealType(1))
Definition uniform_real_distribution.hpp:51
constexpr auto max() const -> result_type
Definition uniform_real_distribution.hpp:66
RealType result_type
Definition uniform_real_distribution.hpp:14
constexpr auto b() const -> result_type
Definition uniform_real_distribution.hpp:62
friend constexpr auto operator==(uniform_real_distribution const &x, uniform_real_distribution const &y) -> bool
Definition uniform_real_distribution.hpp:91
constexpr auto operator()(URBG &g, param_type const &parm) noexcept(noexcept(g())) -> result_type
Definition uniform_real_distribution.hpp:77