tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
bernoulli_distribution.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_RANDOM_BERNOULLI_DISTRIBUTION_HPP
4#define TETL_RANDOM_BERNOULLI_DISTRIBUTION_HPP
5
9
10namespace etl {
11
14 using result_type = bool;
15
16 struct param_type {
18
19 constexpr param_type() noexcept
20 : param_type{0.5}
21 {
22 }
23
24 explicit constexpr param_type(double p) noexcept
25 : probability{p}
26 {
27 }
28
29 [[nodiscard]] constexpr auto p() const noexcept -> double { return probability; }
30
31 [[nodiscard]] friend constexpr auto operator==(param_type const& lhs, param_type const& rhs) noexcept -> bool
32 {
33 return lhs.probability == rhs.probability;
34 }
35
37 };
38
39 constexpr bernoulli_distribution() noexcept
41 {
42 }
43
44 explicit constexpr bernoulli_distribution(double p) noexcept
46 {
47 }
48
49 explicit constexpr bernoulli_distribution(param_type const& parm) noexcept
50 : _param{parm}
51 {
52 }
53
54 [[nodiscard]] constexpr auto p() const noexcept -> double { return _param.p(); }
55
56 constexpr auto param(param_type const& parm) noexcept -> void { _param = parm; }
57
58 [[nodiscard]] constexpr auto param() const noexcept -> param_type { return _param; }
59
60 [[nodiscard]] constexpr auto min() const noexcept -> result_type
61 {
62 (void)this;
63 return false;
64 }
65
66 [[nodiscard]] constexpr auto max() const noexcept -> result_type
67 {
68 (void)this;
69 return true;
70 }
71
72 constexpr auto reset() noexcept -> void { (void)this; }
73
74 template <typename URBG>
75 [[nodiscard]] constexpr auto operator()(URBG& g) noexcept(noexcept(g())) -> result_type
76 {
77 return (*this)(g, _param);
78 }
79
80 template <typename URBG>
81 [[nodiscard]] constexpr auto operator()(URBG& g, param_type const& parm) noexcept(noexcept(g())) -> result_type
82 {
83 constexpr auto digits = static_cast<size_t>(numeric_limits<double>::digits);
84 constexpr auto bits = ~size_t{0};
85 constexpr auto minBits = digits < bits ? digits : bits;
86 static_assert(minBits <= 64);
87
88 return generate_canonical<double, minBits>(g) < parm.p();
89 }
90
91 [[nodiscard]] friend constexpr auto
92 operator==(bernoulli_distribution const& x, bernoulli_distribution const& y) noexcept -> bool
93 {
94 return x.param() == y.param();
95 }
96
97private:
98 param_type _param;
99};
100
101} // namespace etl
102
103#endif // TETL_RANDOM_BERNOULLI_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
Definition bernoulli_distribution.hpp:16
constexpr param_type() noexcept
Definition bernoulli_distribution.hpp:19
double probability
Definition bernoulli_distribution.hpp:36
constexpr auto p() const noexcept -> double
Definition bernoulli_distribution.hpp:29
constexpr param_type(double p) noexcept
Definition bernoulli_distribution.hpp:24
friend constexpr auto operator==(param_type const &lhs, param_type const &rhs) noexcept -> bool
Definition bernoulli_distribution.hpp:31
bernoulli_distribution distribution_type
Definition bernoulli_distribution.hpp:17
constexpr bernoulli_distribution(param_type const &parm) noexcept
Definition bernoulli_distribution.hpp:49
constexpr auto operator()(URBG &g) noexcept(noexcept(g())) -> result_type
Definition bernoulli_distribution.hpp:75
friend constexpr auto operator==(bernoulli_distribution const &x, bernoulli_distribution const &y) noexcept -> bool
Definition bernoulli_distribution.hpp:92
constexpr auto param(param_type const &parm) noexcept -> void
Definition bernoulli_distribution.hpp:56
constexpr auto reset() noexcept -> void
Definition bernoulli_distribution.hpp:72
constexpr auto p() const noexcept -> double
Definition bernoulli_distribution.hpp:54
constexpr auto param() const noexcept -> param_type
Definition bernoulli_distribution.hpp:58
constexpr bernoulli_distribution(double p) noexcept
Definition bernoulli_distribution.hpp:44
bool result_type
Definition bernoulli_distribution.hpp:14
constexpr auto max() const noexcept -> result_type
Definition bernoulli_distribution.hpp:66
constexpr auto min() const noexcept -> result_type
Definition bernoulli_distribution.hpp:60
constexpr auto operator()(URBG &g, param_type const &parm) noexcept(noexcept(g())) -> result_type
Definition bernoulli_distribution.hpp:81
constexpr bernoulli_distribution() noexcept
Definition bernoulli_distribution.hpp:39
static constexpr int digits
Definition numeric_limits.hpp:24