xtd 1.0.0
Loading...
Searching...
No Matches
random.hpp
Go to the documentation of this file.
1
4#pragma once
5
6#include "array.hpp"
7#include "box_integer.hpp"
8#include "core_export.hpp"
9#include "environment.hpp"
10#include "math.hpp"
11#include "numeric_literals.hpp"
12#include "object.hpp"
13#include "optional.hpp"
14#include "span.hpp"
15#include <algorithm>
16#include <limits>
17#include <random>
18
20namespace xtd {
42 class random : public object {
43 public:
45
49
52 explicit random(xtd::uint32 seed);
53
56 explicit random(std::random_device& random_device);
58
60 random(random&&) = default;
61 random(const random&) = default;
62 random& operator =(const random&) = default;
64
66
70 [[nodiscard]] auto generator() const noexcept -> const std::default_random_engine&;
73 [[nodiscard]] auto generator() noexcept -> std::default_random_engine&;
75
77
82 template<typename value_t>
83 [[nodiscard]] auto get_items(const xtd::read_only_span<value_t>& choices, xtd::usize length) -> xtd::array<value_t> {
84 auto result = array<value_t>(length);
85 auto span_result = span<value_t>(result);
86 get_items(choices, span_result);
87 return result;
88 }
89
92 template<typename value_t>
93 [[nodiscard]] auto get_items(const xtd::array<value_t>& choices, xtd::usize length) -> xtd::array<value_t> {
95 }
96
99 template<typename value_t>
100 auto get_items(const xtd::read_only_span<value_t>& choices, xtd::span<value_t>& destination) -> void {
101 for (auto& item : destination)
102 item = choices[next(choices.length())];
103 }
104
107 [[nodiscard]] virtual auto next() const -> xtd::int32;
110 template<typename value_t>
111 [[nodiscard]] auto next() const -> value_t {
113 }
114
120 [[nodiscard]] virtual auto next(xtd::int32 max_value) const -> xtd::int32;
126 template<typename value_t>
127 [[nodiscard]] auto next(value_t max_value) const -> value_t {
128 return static_cast<value_t>(next(value_t {}, max_value));
129 }
130
138 [[nodiscard]] virtual auto next(xtd::int32 min_value, xtd::int32 max_value) const -> xtd::int32;
146 template<typename value_t>
147 [[nodiscard]] auto next(value_t min_value, value_t max_value) const -> value_t {
149 if (min_value == max_value) return min_value;
150 return min_value + static_cast<value_t>(math::round(sample() * (max_value - 1 - min_value)));
151 }
152
154 template<xtd::boolean>
155 [[nodiscard]] auto next() const -> xtd::boolean {return static_cast<xtd::boolean>(next(0, 2));}
156 [[nodiscard]] auto next(xtd::boolean max_value) const -> xtd::boolean;
157 [[nodiscard]] auto next(xtd::decimal max_value) const -> xtd::decimal;
158 [[nodiscard]] auto next(double max_value) const -> double;
159 [[nodiscard]] auto next(xtd::single max_value) const -> xtd::single;
160 [[nodiscard]] auto next(xtd::boolean min_value, xtd::boolean max_value) const -> xtd::boolean;
161 [[nodiscard]] auto next(xtd::decimal min_value, xtd::decimal max_value) const -> xtd::decimal;
162 [[nodiscard]] auto next(double min_value, double max_value) const -> double;
163 [[nodiscard]] auto next(xtd::single min_value, xtd::single max_value) const -> xtd::single;
165
166
169 [[nodiscard]] virtual auto next_byte() const -> xtd::byte;
175 [[nodiscard]] virtual auto next_byte(xtd::byte max_value) const -> xtd::byte;
183 [[nodiscard]] virtual auto next_byte(xtd::byte min_value, xtd::byte max_value) const -> xtd::byte;
184
188 virtual auto next_bytes(xtd::span<xtd::byte>& buffer) const -> void;
192 virtual auto next_bytes(xtd::array<xtd::byte>& buffer) const -> void;
193
197 [[nodiscard]] virtual auto next_double() const -> double;
198
202 template<typename value_t>
203 auto next_values(xtd::span<value_t>& buffer) const -> void {
204 for (auto index = 0_z; index < buffer.length(); ++index)
205 buffer[index] = next<value_t>();
206 }
207
211 template<typename value_t>
212 auto next_values(xtd::array<value_t>& buffer) const -> void {
213 auto span_buffer = span<value_t> {buffer};
214 next_values(span_buffer);
215 }
216
219 [[nodiscard]] virtual auto next_int64() const -> xtd::int64;
225 [[nodiscard]] virtual auto next_int64(int64 max_value) const -> xtd::int64;
233 [[nodiscard]] virtual auto next_int64(int64 min_value, int64 max_value) const -> xtd::int64;
234
237 [[nodiscard]] virtual auto next_single() const -> xtd::single;
238
241 template<typename value_t>
242 auto shuffle(xtd::span<value_t>& values) const -> xtd::span<value_t>& {
243 for (auto index = 0_z; index < values.length() - 1; ++index)
244 std::swap(values[index], values[next(index, values.length())]);
245 return values;
246 }
247
249 template<typename collection_t>
250 auto shuffle(collection_t& values) const -> collection_t& {
251 auto span_values = span<typename collection_t::value_type> {values};
252 shuffle(span_values);
253 return values;
254 }
255
257 template<typename collection_t>
258 auto shuffle(const collection_t& values) const -> collection_t {
259 auto result = values;
260 shuffle(result);
261 return result;
262 }
263
264
265 protected:
269 [[nodiscard]] virtual auto sample() const -> double;
270
271 private:
272 mutable std::default_random_engine generator_;
273 };
274}
Contains xtd::array class.
Contains xtd::box_integer class.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
static constexpr type_t max_value
Represents the largest possible value of type_t. This field is constant.
Definition box_integer.hpp:71
static auto throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current()) -> void
Throws an exption with specified exception case.
static auto round(xtd::decimal value) -> xtd::decimal
Rounds a double-precision floating-point value to the nearest integral value.
object()=default
Create a new instance of the ultimate base class object.
random(xtd::uint32 seed)
Initializes a new instance of the random class, using a specified seed value.
virtual auto next_int64() const -> xtd::int64
Returns a nonnegative random number.
virtual auto next() const -> xtd::int32
Returns a nonnegative random number.
virtual auto next_double() const -> double
Returns a random number between 0.0 and 1.0.
auto get_items(const xtd::read_only_span< value_t > &choices, xtd::usize length) -> xtd::array< value_t >
Creates an array populated with items chosen at random from the provided set of choices.
Definition random.hpp:83
random(std::random_device &random_device)
Initializes a new instance of the random class, using a specified random device value.
virtual auto next_bytes(xtd::span< xtd::byte > &buffer) const -> void
Fills the elements of a specified array of bytes with random numbers.
auto get_items(const xtd::read_only_span< value_t > &choices, xtd::span< value_t > &destination) -> void
Fills the elements of a specified span with items chosen at random from the provided set of choices.
Definition random.hpp:100
auto next_values(xtd::span< value_t > &buffer) const -> void
Fills the elements of a specified xtd::span of bytes with random numbers.
Definition random.hpp:203
auto next(value_t min_value, value_t max_value) const -> value_t
Returns a random number within a specified range.
Definition random.hpp:147
virtual auto next(xtd::int32 min_value, xtd::int32 max_value) const -> xtd::int32
Returns a random number within a specified range.
auto shuffle(xtd::span< value_t > &values) const -> xtd::span< value_t > &
Performs an in-place shuffle of a span.
Definition random.hpp:242
auto get_items(const xtd::array< value_t > &choices, xtd::usize length) -> xtd::array< value_t >
Creates an array populated with items chosen at random from the provided set of choices.
Definition random.hpp:93
auto generator() const noexcept -> const std::default_random_engine &
Gets the underlying generator.
auto next(value_t max_value) const -> value_t
Returns a nonnegative random number less than the specified maximum.
Definition random.hpp:127
virtual auto next(xtd::int32 max_value) const -> xtd::int32
Returns a nonnegative random number less than the specified maximum.
virtual auto sample() const -> double
Returns a random number between 0.0 and 1.0.
virtual auto next_single() const -> xtd::single
Returns a random number between 0.0 and 1.0.
auto next_values(xtd::array< value_t > &buffer) const -> void
Fills the elements of a specified array of bytes with random numbers.
Definition random.hpp:212
auto shuffle(const collection_t &values) const -> collection_t
Performs an in-place shuffle of an array.
Definition random.hpp:258
auto shuffle(collection_t &values) const -> collection_t &
Performs an in-place shuffle of an array.
Definition random.hpp:250
random()
Initializes a new instance of the random class, using a default generated seed value.
virtual auto next_byte() const -> xtd::byte
Returns a nonnegative random number.
Represents a non-owning view over a contiguous sequence of objects.
Definition span.hpp:55
Contains core_export_ keyword.
Contains xtd::environment class.
@ argument_out_of_range
The argument is out of range.
Definition exception_case.hpp:35
std::int64_t int64
Represents a 64-bit signed integer.
Definition int64.hpp:23
std::uint32_t uint32
Represents a 32-bit unsigned integer.
Definition uint32.hpp:23
bool boolean
Represents a boolean.
Definition boolean.hpp:23
long double decimal
Represents a decimal-precision floating-point number.
Definition decimal.hpp:23
std::int32_t int32
Represents a 32-bit signed integer.
Definition int32.hpp:25
std::size_t usize
Represents an unsigned size of any object in bytes.
Definition usize.hpp:22
float single
Represents a single-precision floating-point number.
Definition single.hpp:23
Contains xtd::math class.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
constexpr auto length() const noexcept -> size_type
Returns the length of the current read_only_span.
Definition read_only_span.hpp:213
constexpr read_only_span()
Creates an empty xtd::read_only_span whose xtd::read_only_span::data is null and xtd::read_only_span:...
Definition read_only_span.hpp:85
Contains xtd numeric literals.
Contains xtd::object class.
Contains xtd::optional type.
Contains xtd::span class.