xtd - Reference Guide  0.1.1
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
random.h
Go to the documentation of this file.
1 #pragma once
5 #include <limits>
6 #include <optional>
7 #include <random>
10 #include "core_export.h"
11 #include "environment.h"
12 #include "object.h"
13 
15 namespace xtd {
37  class random : public object {
38  public:
40  random() : generator_(static_cast<uint32_t>(environment::tick_count().count())) {}
41 
44  explicit random(uint32_t seed) : generator_(seed + 1) {}
45 
48  explicit random(std::random_device& random_device) : generator_(random_device()) {}
49 
52  virtual int32_t next() const {return next(std::numeric_limits<int32_t>::max());}
53 
56  template<typename value_t>
57  value_t next() const {return next(std::numeric_limits<value_t>::max());}
58 
64  virtual int32_t next(int32_t max_value) const {return next(0, max_value);}
65 
71  template<typename value_t>
72  value_t next(value_t max_value) const {return next(0, max_value);}
73 
81  virtual int32_t next(int32_t min_value, int32_t max_value) const {
82  if (min_value > max_value) throw argument_out_of_range_exception(current_stack_frame_);
83  if (min_value == max_value) return min_value;
84  return min_value + static_cast<int32_t>(std::round(sample() * std::numeric_limits<int32_t>::max())) % ((max_value - 1) - min_value + 1);
85  }
86 
94  template<typename value_t>
95  value_t next(value_t min_value, value_t max_value) const {
96  if (min_value > max_value) throw argument_out_of_range_exception(current_stack_frame_);
97  if (min_value == max_value) return min_value;
98  return min_value + static_cast<value_t>(std::round(sample() * std::numeric_limits<value_t>::max())) % ((max_value - 1) - min_value + 1);
99  }
100 
104  virtual void next_bytes(std::vector<uint8_t>& buffer) const {next_bytes(buffer.data(), buffer.size());}
105 
110  virtual void next_bytes(uint8_t* buffer, size_t buffer_size) const {
111  if (buffer == nullptr) throw argument_null_exception(current_stack_frame_);
112  for (size_t index = 0; index < buffer_size; index++)
113  buffer[index] = next<uint8_t>(0, std::numeric_limits<uint8_t>::max());
114  }
115 
119  template<typename value_t>
120  void next_values(std::vector<value_t>& buffer) const {next_values(buffer.data(), buffer.size());}
121 
126  template<typename value_t>
127  void next_values(value_t* buffer, size_t buffer_size) const {
128  if (buffer == nullptr) throw argument_null_exception(current_stack_frame_);
129  for (size_t index = 0; index < buffer_size; index++)
130  buffer[index] = next<value_t>(0, std::numeric_limits<value_t>::max());
131  }
132 
136  virtual double next_double() const{return sample();}
137 
138  protected:
142  virtual double sample() const {
143  static std::uniform_real_distribution<> distribution_(0, 1);
144  return distribution_(generator_);
145  }
146 
147  private:
148  mutable std::default_random_engine generator_;
149  };
150 }
Contains xtd::argument_null_exception exception.
Contains xtd::argument_out_of_range_exception exception.
The exception that is thrown when one of the arguments provided to a method is null.
Definition: argument_null_exception.h:18
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:18
The environment class.
Definition: environment.h:33
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition: object.h:26
Represents a pseudo-random number generator, a device that produces a sequence of numbers that meet c...
Definition: random.h:37
value_t next(value_t max_value) const
Returns a nonnegative random number less than the specified maximum.
Definition: random.h:72
virtual void next_bytes(std::vector< uint8_t > &buffer) const
Fills the elements of a specified array of bytes with random numbers.
Definition: random.h:104
virtual void next_bytes(uint8_t *buffer, size_t buffer_size) const
Fills the elements of a specified array of bytes with random numbers.
Definition: random.h:110
random(uint32_t seed)
Initializes a new instance of the random class, using a specified seed value.
Definition: random.h:44
virtual double next_double() const
Returns a random number between 0.0 and 1.0.
Definition: random.h:136
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:127
virtual int32_t next(int32_t min_value, int32_t max_value) const
Returns a random number within a specified range.
Definition: random.h:81
random(std::random_device &random_device)
Initializes a new instance of the random class, using a specified random device value.
Definition: random.h:48
virtual int32_t next(int32_t max_value) const
Returns a nonnegative random number less than the specified maximum.
Definition: random.h:64
value_t next(value_t min_value, value_t max_value) const
Returns a random number within a specified range.
Definition: random.h:95
value_t next() const
Returns a nonnegative random number.
Definition: random.h:57
void next_values(std::vector< value_t > &buffer) const
Fills the elements of a specified array of bytes with random numbers.
Definition: random.h:120
random()
Initializes a new instance of the random class, using a default generated seed value.
Definition: random.h:40
virtual int32_t next() const
Returns a nonnegative random number.
Definition: random.h:52
virtual double sample() const
Returns a random number between 0.0 and 1.0.
Definition: random.h:142
Contains core_export_ keyword.
Contains xtd::environment class.
#define current_stack_frame_
Provides information about the current stack frame.
Definition: stack_frame.h:201
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition: system_report.h:17
Contains xtd::object class.