56 explicit random(std::random_device& random_device);
70 [[nodiscard]]
auto generator() const noexcept -> const std::default_random_engine&;
73 [[nodiscard]] auto
generator() noexcept -> std::default_random_engine&;
82 template<typename value_t>
92 template<
typename value_t>
99 template<
typename value_t>
101 for (
auto& item : destination)
102 item = choices[
next(choices.length())];
110 template<typename value_t>
111 [[nodiscard]] auto
next() const -> value_t {
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));
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)));
154 template<xtd::
boolean>
158 [[nodiscard]]
auto next(
double max_value)
const -> double;
162 [[nodiscard]]
auto next(
double min_value,
double max_value)
const -> double;
202 template<typename value_t>
204 for (
auto index = 0_z; index < buffer.length(); ++index)
211 template<
typename value_t>
241 template<typename value_t>
243 for (
auto index = 0_z; index < values.length() - 1; ++index)
244 std::swap(values[index], values[
next(index, values.length())]);
249 template<
typename collection_t>
250 auto shuffle(collection_t& values)
const -> collection_t& {
257 template<
typename collection_t>
258 auto shuffle(
const collection_t& values)
const -> collection_t {
259 auto result = values;
269 [[nodiscard]]
virtual auto sample() const ->
double;
272 mutable std::default_random_engine generator_;
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.