xtd 0.2.0
random.hpp
Go to the documentation of this file.
1
4#pragma once
5
8#include "array.hpp"
9#include "box_integer.hpp"
10#include "core_export.hpp"
11#include "environment.hpp"
12#include "math.hpp"
13#include "numeric_literals.hpp"
14#include "object.hpp"
15#include "optional.hpp"
16#include "span.hpp"
17#include <algorithm>
18#include <limits>
19#include <random>
20
22namespace xtd {
44 class random : public object {
45 public:
47
51
54 explicit random(uint32 seed);
55
58 explicit random(std::random_device& random_device);
60
62 random(random&&) = default;
63 random(const random&) = default;
64 random& operator =(const random&) = default;
66
68
72 const std::default_random_engine& generator() const noexcept;
75 std::default_random_engine& generator() noexcept;
77
79
84 template<class value_t>
85 xtd::array<value_t> get_items(const xtd::read_only_span<value_t>& choices, xtd::size length) {
86 auto result = array<value_t>(length);
87 auto span_result = span<value_t>(result);
88 get_items(choices, span_result);
89 return result;
90 }
91
94 template<class value_t>
98
101 template<class value_t>
102 void get_items(const xtd::read_only_span<value_t>& choices, xtd::span<value_t>& destination) {
103 for (auto& item : destination)
104 item = choices[next(choices.size())];
105 }
106
109 virtual int32 next() const;
112 template<class value_t>
113 value_t next() const {
115 }
116
122 virtual int32 next(int32 max_value) const;
128 template<class value_t>
129 value_t next(value_t max_value) const {
130 return static_cast<value_t>(next(value_t {}, max_value));
131 }
132
140 virtual int32 next(int32 min_value, int32 max_value) const;
148 template<class value_t>
149 value_t next(value_t min_value, value_t max_value) const {
151 if (min_value == max_value) return min_value;
152 return min_value + static_cast<value_t>(math::round(sample() * (max_value - 1 - min_value)));
153 }
154
156 template<xtd::boolean>
157 xtd::boolean next() const {return static_cast<xtd::boolean>(next(0, 2));}
158 xtd::boolean next(xtd::boolean max_value) const;
159 decimal next(decimal max_value) const;
160 double next(double max_value) const;
161 float next(float max_value) const;
162 xtd::boolean next(xtd::boolean min_value, xtd::boolean max_value) const;
163 decimal next(decimal min_value, decimal max_value) const;
164 double next(double min_value, double max_value) const;
165 float next(float min_value, float max_value) const;
167
171 virtual void next_bytes(xtd::span<xtd::byte>& buffer) const;
175 virtual void next_bytes(xtd::array<xtd::byte>& buffer) const;
176
180 virtual double next_double() const;
181
185 template<class value_t>
186 void next_values(xtd::span<value_t>& buffer) const {
187 for (auto index = 0_z; index < buffer.size(); ++index)
188 buffer[index] = next<value_t>();
189 }
190
194 template<class value_t>
195 void next_values(xtd::array<value_t>& buffer) const {
196 auto span_buffer = span<value_t> {buffer};
197 next_values(span_buffer);
198 }
199
202 virtual int64 next_int64() const;
208 virtual int64 next_int64(int64 max_value) const;
216 virtual int64 next_int64(int64 min_value, int64 max_value) const;
217
220 virtual single next_single() const;
221
224 template<class value_t>
225 void shuffle(xtd::span<value_t>& values) const {
226 for (auto index = 0_z; index < values.length() - 1; ++index)
227 std::swap(values[index], values[next(index, values.length())]);
228 }
229
231 template<class collection_t>
232 void shuffle(collection_t& values) const {
233 auto span_values = span<typename collection_t::value_type> {values};
234 shuffle(span_values);
235 }
236
237
238 protected:
242 virtual double sample() const;
243
244 private:
245 mutable std::default_random_engine generator_;
246 };
247}
Contains xtd::argument_null_exception exception.
Contains xtd::argument_out_of_range_exception exception.
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:61
static constexpr type_t max_value
Represents the largest possible value of type_t. This field is constant.
Definition box_integer.hpp:67
static void throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current())
Throws an exption with specified exception case.
static decimal round(decimal value)
Rounds a double-precision floating-point value to the nearest integral value.
object()=default
Create a new instance of the ultimate base class object.
value_t next(value_t max_value) const
Returns a nonnegative random number less than the specified maximum.
Definition random.hpp:129
virtual int32 next(int32 min_value, int32 max_value) const
Returns a random number within a specified range.
random(uint32 seed)
Initializes a new instance of the random class, using a specified seed value.
void shuffle(collection_t &values) const
Performs an in-place shuffle of an array.
Definition random.hpp:232
virtual int32 next(int32 max_value) const
Returns a nonnegative random number less than the specified maximum.
void get_items(const xtd::read_only_span< value_t > &choices, xtd::span< value_t > &destination)
Fills the elements of a specified span with items chosen at random from the provided set of choices.
Definition random.hpp:102
virtual double next_double() const
Returns a random number between 0.0 and 1.0.
virtual single next_single() const
Returns a random number between 0.0 and 1.0.
xtd::array< value_t > get_items(const xtd::read_only_span< value_t > &choices, xtd::size length)
Creates an array populated with items chosen at random from the provided set of choices.
Definition random.hpp:85
virtual int64 next_int64(int64 min_value, int64 max_value) const
Returns a random number within a specified range.
random(std::random_device &random_device)
Initializes a new instance of the random class, using a specified random device value.
virtual int32 next() const
Returns a nonnegative random number.
const std::default_random_engine & generator() const noexcept
Gets the underlying generator.
xtd::array< value_t > get_items(const xtd::array< value_t > &choices, xtd::size length)
Creates an array populated with items chosen at random from the provided set of choices.
Definition random.hpp:95
virtual int64 next_int64(int64 max_value) const
Returns a nonnegative random number less than the specified maximum.
value_t next(value_t min_value, value_t max_value) const
Returns a random number within a specified range.
Definition random.hpp:149
value_t next() const
Returns a nonnegative random number.
Definition random.hpp:113
virtual int64 next_int64() const
Returns a nonnegative random number.
void next_values(xtd::span< value_t > &buffer) const
Fills the elements of a specified xtd::span of bytes with random numbers.
Definition random.hpp:186
virtual void next_bytes(xtd::array< xtd::byte > &buffer) const
Fills the elements of a specified array of bytes with random numbers.
void next_values(xtd::array< value_t > &buffer) const
Fills the elements of a specified array of bytes with random numbers.
Definition random.hpp:195
random()
Initializes a new instance of the random class, using a default generated seed value.
virtual double sample() const
Returns a random number between 0.0 and 1.0.
virtual void next_bytes(xtd::span< xtd::byte > &buffer) const
Fills the elements of a specified array of bytes with random numbers.
void shuffle(xtd::span< value_t > &values) const
Performs an in-place shuffle of a span.
Definition random.hpp:225
Represents a non-owning view over a contiguous sequence of objects.
Definition span.hpp:58
constexpr size_type length() const noexcept
Returns the length of the current span.
Definition span.hpp:253
constexpr size_type size() const noexcept
Returns the number of elements.
Definition span.hpp:271
Contains core_export_ keyword.
Contains xtd::environment class.
@ argument_out_of_range
The argument is out of range.
Definition exception_case.hpp:35
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
int32_t int32
Represents a 32-bit signed integer.
Definition int32.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
int64_t int64
Represents a 64-bit signed integer.
Definition int64.hpp:23
uint32_t uint32
Represents a 32-bit unsigned integer.
Definition uint32.hpp:23
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 read_only_span(const element_type(&array)[len]) noexcept
Creates an xtd::read_only_span with specified native array.
Definition read_only_span.hpp:112
constexpr size_type length() const noexcept
Returns the length of the current read_only_span.
Definition read_only_span.hpp:229
Contains xtd numeric literals.
Contains xtd::object class.
Contains xtd::optional type.
Contains xtd::span class.