xtd 0.2.0
random.h
Go to the documentation of this file.
1 #pragma once
7 #include "core_export.h"
8 #include "environment.h"
9 #include "math.h"
10 #include "object.h"
11 #include "optional.h"
12 #include <limits>
13 #include <random>
14 
16 namespace xtd {
38  class random : public object {
39  public:
41 
44  random();
45 
48  explicit random(uint32 seed);
49 
52  explicit random(std::random_device& random_device);
54 
56  random(random&&) = default;
57  random(const random&) = default;
58  random& operator =(const random&) = default;
60 
62 
66  std::default_random_engine generator() const noexcept;
68 
70 
74  virtual int32 next() const;
75 
78  template<typename value_t>
79  value_t next() const {
80  return next(std::numeric_limits<value_t>::max());
81  }
82 
88  virtual int32 next(int32 max_value) const;
89 
95  template<typename value_t>
96  value_t next(value_t max_value) const {
97  return next(0, max_value);
98  }
99 
107  virtual int32 next(int32 min_value, int32 max_value) const;
108 
116  template<typename value_t>
117  value_t next(value_t min_value, value_t max_value) const {
118  if (min_value > max_value) throw argument_out_of_range_exception {csf_};
119  if (min_value == max_value) return min_value;
120  return min_value + static_cast<value_t>(math::round(sample() * std::numeric_limits<value_t>::max())) % ((max_value - 1) - min_value + 1);
121  }
122 
124  double next(decimal max_value) const;
125  double next(double max_value) const;
126  float next(float max_value) const;
127  double next(decimal min_value, decimal max_value) const;
128  double next(double min_value, double max_value) const;
129  float next(float min_value, float max_value) const;
131 
135  virtual void next_bytes(std::vector<xtd::byte>& buffer) const;
136 
141  virtual void next_bytes(xtd::byte* buffer, size_t buffer_size) const;
142 
146  template<typename value_t>
147  void next_values(std::vector<value_t>& buffer) const {
148  next_values(buffer.data(), buffer.size());
149  }
150 
155  template<typename value_t>
156  void next_values(value_t* buffer, size_t buffer_size) const {
157  if (buffer == nullptr) throw argument_null_exception {csf_};
158  for (size_t index = 0; index < buffer_size; index++)
159  buffer[index] = next<value_t>(0, std::numeric_limits<value_t>::max());
160  }
161 
165  virtual double next_double() const;
167 
168  protected:
172  virtual double sample() const;
173 
174  private:
175  mutable std::default_random_engine generator_;
176  };
177 }
Contains xtd::math class.
The exception that is thrown when one of the arguments provided to a method is null.
Definition: argument_null_exception.h:20
value_t next() const
Returns a nonnegative random number.
Definition: random.h:79
Contains xtd::argument_out_of_range_exception exception.
Contains std::optional type and std::bad_optional_access exception.
random()
Initializes a new instance of the random class, using a default generated seed value.
value_t next(value_t min_value, value_t max_value) const
Returns a random number within a specified range.
Definition: random.h:117
long double decimal
Represents a decimal-precision floating-point number.
Definition: types.h:96
static decimal round(decimal value)
Rounds a double-precision floating-point value to the nearest integral value.
virtual double next_double() const
Returns a random number between 0.0 and 1.0.
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
#define csf_
Provides information about the current stack frame.
Definition: current_stack_frame.h:30
virtual int32 next() const
Returns a nonnegative random number.
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.h:156
Represents a pseudo-random number generator, a device that produces a sequence of numbers that meet c...
Definition: random.h:38
std::default_random_engine generator() const noexcept
Gets the underlying generator.
Contains xtd::argument_null_exception exception.
The exception that is thrown when one of the arguments provided to a method is out of range...
Definition: argument_out_of_range_exception.h:20
Contains xtd::object class.
virtual double sample() const
Returns a random number between 0.0 and 1.0.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes...
Definition: object.h:32
virtual void next_bytes(std::vector< xtd::byte > &buffer) const
Fills the elements of a specified array of bytes with random numbers.
int_least32_t int32
Represents a 32-bit signed integer.
Definition: types.h:129
Contains xtd::environment class.
uint_least8_t byte
Represents a 8-bit unsigned integer.
Definition: types.h:39
uint_least32_t uint32
Represents a 32-bit unsigned integer.
Definition: types.h:239
void next_values(std::vector< value_t > &buffer) const
Fills the elements of a specified array of bytes with random numbers.
Definition: random.h:147
value_t next(value_t max_value) const
Returns a nonnegative random number less than the specified maximum.
Definition: random.h:96