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 "object.hpp"
14#include "optional.hpp"
15#include "span.hpp"
16#include <algorithm>
17#include <limits>
18#include <random>
19
21namespace xtd {
43 class random : public object {
44 public:
46
50
53 explicit random(uint32 seed);
54
57 explicit random(std::random_device& random_device);
59
61 random(random&&) = default;
62 random(const random&) = default;
63 random& operator =(const random&) = default;
65
67
71 const std::default_random_engine& generator() const noexcept;
74 std::default_random_engine& generator() noexcept;
76
78
83 template<class value_t>
84 xtd::array<value_t> get_items(const xtd::read_only_span<value_t>& choices, xtd::size length) {
85 auto result = array<value_t>(length);
86 auto span_result = span<value_t>(result);
87 get_items(choices, span_result);
88 return result;
89 }
93 template<class value_t>
95 return get_items(read_only_span<value_t>(choices), length);
96 }
100 template<class value_t>
101 void get_items(const xtd::read_only_span<value_t>& choices, xtd::span<value_t>& destination) {
102 for (auto& item : destination)
103 item = choices[next(choices.size())];
104 }
105
108 virtual int32 next() const;
111 template<class value_t>
112 value_t next() const {
114 }
115
121 virtual int32 next(int32 max_value) const;
127 template<class value_t>
128 value_t next(value_t max_value) const {
129 return static_cast<value_t>(next(value_t {}, max_value));
130 }
131
139 virtual int32 next(int32 min_value, int32 max_value) const;
147 template<class value_t>
148 value_t next(value_t min_value, value_t max_value) const {
150 if (min_value == max_value) return min_value;
151 return min_value + static_cast<value_t>(math::round(sample() * (max_value - 1 - min_value)));
152 }
153
155 template<xtd::boolean>
156 xtd::boolean next() const {return static_cast<xtd::boolean>(next(0, 2));}
157 xtd::boolean next(xtd::boolean max_value) const;
158 decimal next(decimal max_value) const;
159 double next(double max_value) const;
160 float next(float max_value) const;
161 xtd::boolean next(xtd::boolean min_value, xtd::boolean max_value) const;
162 decimal next(decimal min_value, decimal max_value) const;
163 double next(double min_value, double max_value) const;
164 float next(float min_value, float max_value) const;
166
170 virtual void next_bytes(xtd::span<xtd::byte>& buffer) const;
174 virtual void next_bytes(xtd::array<xtd::byte>& buffer) const;
175
179 virtual double next_double() const;
180
184 template<class value_t>
185 void next_values(xtd::span<value_t>& buffer) const {
186 for (auto index = 0_z; index < buffer.size(); ++index)
187 buffer[index] = next<value_t>();
188 }
189
193 template<class value_t>
194 void next_values(xtd::array<value_t>& buffer) const {
195 auto span_buffer = span<value_t> {buffer};
196 next_values(span_buffer);
197 }
198
201 virtual int64 next_int64() const;
207 virtual int64 next_int64(int64 max_value) const;
215 virtual int64 next_int64(int64 min_value, int64 max_value) const;
216
219 virtual single next_single() const;
220
223 template<class value_t>
224 void shuffle(xtd::span<value_t>& values) const {
225 for (auto index = 0_z; index < values.length() - 1; ++index)
226 std::swap(values[index], values[next(index, values.length())]);
227 }
230 template<class collection_t>
231 void shuffle(collection_t& values) const {
232 auto span_values = span<typename collection_t::value_type> {values};
233 shuffle(span_values);
234 }
236
237 protected:
241 virtual double sample() const;
242
243 private:
244 mutable std::default_random_engine generator_;
245 };
246}
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
Represents a boxed integer object.
Definition box_integer.hpp:52
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.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:43
Represents a pseudo-random number generator, a device that produces a sequence of numbers that meet c...
Definition random.hpp:43
value_t next(value_t max_value) const
Returns a nonnegative random number less than the specified maximum.
Definition random.hpp:128
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:231
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:101
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:84
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:94
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:148
value_t next() const
Returns a nonnegative random number.
Definition random.hpp:112
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:185
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:194
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:224
Represents a non-owning view over a contiguous sequence of objects.
Definition read_only_span.hpp:52
constexpr size_type size() const noexcept
Returns the number of elements.
Definition read_only_span.hpp:241
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.
int32_t int32
Represents a 32-bit signed integer.
Definition int32.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
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
bool boolean
Represents a boolean.
Definition boolean.hpp:23
Contains xtd::math class.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10
Contains xtd::object class.
Contains xtd::optional type.
Contains xtd::span class.