xtd 0.2.0
Loading...
Searching...
No Matches
random.hpp
Go to the documentation of this file.
1
4#pragma once
5
8#include "box_integer.hpp"
9#include "core_export.hpp"
10#include "environment.hpp"
11#include "math.hpp"
12#include "object.hpp"
13#include "optional.hpp"
14#include <limits>
15#include <random>
16
18namespace xtd {
40 class random : public object {
41 public:
43
47
50 explicit random(uint32 seed);
51
54 explicit random(std::random_device& random_device);
56
58 random(random&&) = default;
59 random(const random&) = default;
60 random& operator =(const random&) = default;
62
64
68 std::default_random_engine generator() const noexcept;
70
72
76 virtual int32 next() const;
77
80 template<typename value_t>
81 value_t next() const {
83 }
84
90 virtual int32 next(int32 max_value) const;
91
97 template<typename value_t>
98 value_t next(value_t max_value) const {
99 return static_cast<value_t>(next(value_t {}, max_value));
100 }
101
109 virtual int32 next(int32 min_value, int32 max_value) const;
110
118 template<typename value_t>
119 value_t next(value_t min_value, value_t max_value) const {
120 if (min_value > max_value) throw argument_out_of_range_exception {};
121 if (min_value == max_value) return min_value;
122 return min_value + static_cast<value_t>(math::round(sample() * xtd::box_integer<value_t>::max_value)) % ((max_value - 1) - min_value + 1);
123 }
124
126 decimal next(decimal max_value) const;
127 double next(double max_value) const;
128 float next(float max_value) const;
129 decimal next(decimal min_value, decimal max_value) const;
130 double next(double min_value, double max_value) const;
131 float next(float min_value, float max_value) const;
133
137 virtual void next_bytes(std::vector<xtd::byte>& buffer) const;
138
143 virtual void next_bytes(xtd::byte* buffer, size_t buffer_size) const;
144
148 template<typename value_t>
149 void next_values(std::vector<value_t>& buffer) const {
150 next_values(buffer.data(), buffer.size());
151 }
152
157 template<typename value_t>
158 void next_values(value_t* buffer, size_t buffer_size) const {
159 if (buffer == nullptr) throw argument_null_exception {};
160 for (size_t index = 0; index < buffer_size; index++)
161 buffer[index] = next<value_t>(0, xtd::box_integer<value_t>::max_value);
162 }
163
167 virtual double next_double() const;
169
170 protected:
174 virtual double sample() const;
175
176 private:
177 mutable std::default_random_engine generator_;
178 };
179}
Contains xtd::argument_null_exception exception.
Contains xtd::argument_out_of_range_exception exception.
Contains xtd::box_integer class.
The exception that is thrown when one of the arguments provided to a method is null.
Definition argument_null_exception.hpp:23
The exception that is thrown when one of the arguments provided to a method is out of range.
Definition argument_out_of_range_exception.hpp:23
Represents a boxed integer object.
Definition box_integer.hpp:52
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:42
Represents a pseudo-random number generator, a device that produces a sequence of numbers that meet c...
Definition random.hpp:40
value_t next(value_t max_value) const
Returns a nonnegative random number less than the specified maximum.
Definition random.hpp:98
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.
virtual void next_bytes(std::vector< xtd::byte > &buffer) const
Fills the elements of a specified array of bytes with random numbers.
virtual int32 next(int32 max_value) const
Returns a nonnegative random number less than the specified maximum.
virtual void next_bytes(xtd::byte *buffer, size_t buffer_size) const
Fills the elements of a specified array of bytes with random numbers.
virtual double next_double() const
Returns a random number between 0.0 and 1.0.
void next_values(value_t *buffer, size_t buffer_size) const
Fills the elements of a specified array of bytes with random numbers.
Definition random.hpp:158
random(std::random_device &random_device)
Initializes a new instance of the random class, using a specified random device value.
std::default_random_engine generator() const noexcept
Gets the underlying generator.
virtual int32 next() const
Returns a nonnegative random number.
value_t next(value_t min_value, value_t max_value) const
Returns a random number within a specified range.
Definition random.hpp:119
void next_values(std::vector< value_t > &buffer) const
Fills the elements of a specified array of bytes with random numbers.
Definition random.hpp:149
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.
Contains core_export_ keyword.
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
uint32_t uint32
Represents a 32-bit unsigned integer.
Definition uint32.hpp:23
uint8_t byte
Represents a 8-bit unsigned integer.
Definition byte.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::environment class.