libfxd 0.2.dev
A fixed-point library for C++.
Loading...
Searching...
No Matches
random.hpp
Go to the documentation of this file.
1/*
2 * libfxd - a fixed-point library for C++
3 *
4 * Copyright 2023 Daniel K. O.
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef LIBFXD_RANDOM_HPP
9#define LIBFXD_RANDOM_HPP
10
11#include <istream>
12#include <ostream>
13#include <random>
14
15#include "concepts.hpp"
16#include "limits.hpp"
17
18
19namespace fxd {
20
22 template<fixed_point Fxd>
24 private std::uniform_int_distribution<typename Fxd::raw_type> {
25
26 using parent = std::uniform_int_distribution<typename Fxd::raw_type>;
27
28 public:
29
31 using result_type = Fxd;
32
34 class param_type {
35
36 result_type a_, b_;
37
38 public:
39
40 constexpr
42 param_type{std::numeric_limits<result_type>::lowest()}
43 {}
44
50 explicit
51 constexpr
53 result_type b_ = std::numeric_limits<result_type>::max()) :
54 a_{a_},
55 b_{b_}
56 {}
57
59 [[nodiscard]]
60 constexpr result_type a() const noexcept { return a_; }
61
63 [[nodiscard]]
64 constexpr result_type b() const noexcept { return b_; }
65
67 bool operator ==(const param_type& other) const noexcept = default;
68
69 };
70
71
74 uniform_real_distribution{std::numeric_limits<result_type>::lowest()}
75 {}
76
77
83 explicit
85 result_type b_ = std::numeric_limits<result_type>::max()) :
86 parent{a_.raw_value, b_.raw_value}
87 {}
88
89
91 explicit
93 parent{typename parent::param_type{p.a().raw_value, p.b().raw_value}}
94 {}
95
96
98 void reset() noexcept {}
99
100
104 const noexcept
105 {
106 return result_type::from_raw(parent::a());
107 }
108
109
113 const noexcept
114 {
115 return result_type::from_raw(parent::b());
116 }
117
118
120 [[nodiscard]]
121 param_type
123 const noexcept
124 {
125 return {a(), b()};
126 }
127
128
130 void
132 {
133 parent::param(typename parent::param_type{p.a().raw_value,
134 p.b().raw_value});
135 }
136
137
139 [[nodiscard]]
142 const noexcept
143 {
144 return a();
145 }
146
147
149 [[nodiscard]]
152 const noexcept
153 {
154 return b();
155 }
156
157
159 template<typename Gen>
160 [[nodiscard]]
163 {
164 return result_type::from_raw(parent::operator()(g));
165 }
166
167
169 template<typename Gen>
170 [[nodiscard]]
172 operator ()(Gen& g, const param_type& p)
173 {
174 typename parent::param_type pp{p.a().raw_value, p.b().raw_value};
175 return result_type::from_raw(parent::operator()(g, pp));
176 }
177
178
180 bool operator ==(const uniform_real_distribution&) const = default;
181
182
184 template<typename CharT,
185 typename Traits>
186 friend
187 std::basic_ostream<CharT, Traits>&
188 operator <<(std::basic_ostream<CharT, Traits>& out,
189 const uniform_real_distribution& dist)
190 {
191 const auto flags = out.flags();
192 const auto fill = out.fill();
193 const auto prec = out.precision();
194 const auto space = out.widen(' ');
195
196 out << std::scientific << std::left;
197 out.fill(space);
198 out.precision(std::numeric_limits<result_type>::max_digits10);
199
200 try {
201 out << dist.a() << space << dist.b();
202 out.flags(flags);
203 out.fill(fill);
204 out.precision(prec);
205 return out;
206 }
207 catch (...) {
208 out.flags(flags);
209 out.fill(fill);
210 out.precision(prec);
211 throw;
212 }
213 }
214
215
217 template<typename CharT,
218 typename Traits>
219 friend
220 std::basic_istream<CharT, Traits>&
221 operator >>(std::basic_istream<CharT, Traits>& in,
222 const uniform_real_distribution& dist)
223 {
224 const auto flags = in.flags();
225 in >> std::skipws;
226
227 result_type a, b;
228 try {
229 if (in >> a >> b)
230 dist.params({a, b});
231 in.flags(flags);
232 return in;
233 }
234 catch (...) {
235 in.flags(flags);
236 throw;
237 }
238 }
239
240
241 };
242
243
244}
245
246
247#endif
Analogous to std::uniform_real_distribution::param_type
Definition: random.hpp:34
constexpr result_type b() const noexcept
Maximum value.
Definition: random.hpp:64
constexpr result_type a() const noexcept
Minimum value.
Definition: random.hpp:60
bool operator==(const param_type &other) const noexcept=default
Defaulted == operator.
constexpr param_type(result_type a_, result_type b_=std::numeric_limits< result_type >::max())
Construct from min and max range.
Definition: random.hpp:52
Analogous to std::uniform_real_distribution.
Definition: random.hpp:24
Fxd result_type
The type that will be generated.
Definition: random.hpp:31
bool operator==(const uniform_real_distribution &) const =default
Defaulted == operator.
void reset() noexcept
Reset distribution state.
Definition: random.hpp:98
void param(const param_type &p)
Set distribution parameters.
Definition: random.hpp:131
uniform_real_distribution(result_type a_, result_type b_=std::numeric_limits< result_type >::max())
Constructor with min and max values.
Definition: random.hpp:84
friend std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &in, const uniform_real_distribution &dist)
Input operator.
Definition: random.hpp:221
result_type operator()(Gen &g)
Generate a random value.
Definition: random.hpp:162
result_type a() const noexcept
Same as min().
Definition: random.hpp:103
uniform_real_distribution()
Default constructor.
Definition: random.hpp:73
uniform_real_distribution(const param_type &p)
Constructor from a param_type object.
Definition: random.hpp:92
param_type param() const noexcept
Obtain distribution parameters.
Definition: random.hpp:122
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &out, const uniform_real_distribution &dist)
Output operator.
Definition: random.hpp:188
result_type max() const noexcept
Return upper bound of distribution.
Definition: random.hpp:151
result_type b() const noexcept
Same as max().
Definition: random.hpp:112
result_type min() const noexcept
Return lower bound of distribution.
Definition: random.hpp:141
This is the namespace where the entire library is defined.
Definition: casting.hpp:19
STL namespace.