#include <random.h>
Represents a pseudo-random number generator, a device that produces a sequence of numbers that meet certain statistical requirements for randomness.
- Namespace
- xtd
- Library
- xtd.core
- Notes to Callers
- The implementation of the random number generator in the random class is not guaranteed to remain the same across major versions of the xtd. As a result, your application code should not assume that the same seed will result in the same pseudo-random sequence in different versions of the xtd.
- Notes to Inheritors
- In xtd, the behavior of the random::next(), random::next(int32, int32), and next_bytes methods have changed so that these methods do not necessarily call the derived class implementation of the sample method. As a result, classes derived from Random that target the xtd should also virtual these three methods.
- Examples
- The following example creates a single random number generator and calls its next_bytes, next, and next_double methods to generate sequences of random numbers within different ranges.
#include <xtd/xtd>
using namespace std;
auto main()->int {
vector<unsigned char> bytes(5);
for (unsigned char byte_value : bytes)
for (int ctr = 0; ctr <= 4; ctr++)
for (int ctr = 0; ctr <= 4; ctr++)
for (int ctr = 0; ctr <= 4; ctr++)
for (int ctr = 0; ctr <= 4; ctr++)
for (int ctr = 0; ctr <= 4; ctr++)
}
- Examples
- The following example generates a random integer that it uses as an index to retrieve a string value from an array.
#include <xtd/xtd>
using namespace std;
auto main()->int {
vector<ustring> male_pet_names = {"Rufus", "Bear", "Dakota", "Fido", "Vanya", "Samuel", "Koani", "Volodya", "Prince", "Yiska"};
vector<ustring> female_pet_names = {"Maggie", "Penny", "Saya", "Princess", "Abby", "Laila", "Sadie", "Olivia", "Starlight", "Talla"};
size_t male_index = rnd.
next(male_pet_names.size());
size_t female_index = rnd.
next(female_pet_names.size());
}
- Examples:
- as.cpp, change_color.cpp, draw_point.cpp, interlocked.cpp, minesweeper.cpp, random1.cpp, random2.cpp, and random3.cpp.
|
virtual double | sample () const |
| Returns a random number between 0.0 and 1.0. More...
|
|
|
| random () |
| Initializes a new instance of the random class, using a default generated seed value. More...
|
|
| random (uint32 seed) |
| Initializes a new instance of the random class, using a specified seed value. More...
|
|
| random (std::random_device &random_device) |
| Initializes a new instance of the random class, using a specified random device value. More...
|
|
|
std::default_random_engine | generator () const noexcept |
| Gets the underlying generator. More...
|
|
|
virtual int32 | next () const |
| Returns a nonnegative random number. More...
|
|
template<typename value_t > |
value_t | next () const |
| Returns a nonnegative random number. More...
|
|
virtual int32 | next (int32 max_value) const |
| Returns a nonnegative random number less than the specified maximum. More...
|
|
template<typename value_t > |
value_t | next (value_t max_value) const |
| Returns a nonnegative random number less than the specified maximum. More...
|
|
virtual int32 | next (int32 min_value, int32 max_value) const |
| Returns a random number within a specified range. More...
|
|
template<typename value_t > |
value_t | next (value_t min_value, value_t max_value) const |
| Returns a random number within a specified range. More...
|
|
virtual void | next_bytes (std::vector< xtd::byte > &buffer) const |
| Fills the elements of a specified array of bytes with random numbers. More...
|
|
virtual void | next_bytes (xtd::byte *buffer, size_t buffer_size) const |
| Fills the elements of a specified array of bytes with random numbers. More...
|
|
template<typename value_t > |
void | next_values (std::vector< value_t > &buffer) const |
| Fills the elements of a specified array of bytes with random numbers. More...
|
|
template<typename value_t > |
void | next_values (value_t *buffer, size_t buffer_size) const |
| Fills the elements of a specified array of bytes with random numbers. More...
|
|
virtual double | next_double () const |
| Returns a random number between 0.0 and 1.0. More...
|
|
|
| object ()=default |
| Create a new instance of the ultimate base class object. More...
|
|
bool | equals (const object &obj) const noexcept |
| Determines whether the specified object is equal to the current object. More...
|
|
virtual size_t | get_hash_code () const noexcept |
| Serves as a hash function for a particular type. More...
|
|
template<typename object_t > |
std::unique_ptr< object_t > | memberwise_clone () const noexcept |
| Gets the type of the current instance. More...
|
|
virtual xtd::ustring | to_string () const noexcept |
| Returns a sxd::ustring that represents the current object. More...
|
|
static bool | equals (const object &object_a, const object &object_b) noexcept |
| Determines whether the specified object instances are considered equal. More...
|
|
static bool | reference_equals (const object &object_a, const object &object_b) noexcept |
| Determines whether the specified object instances are the same instance. More...
|
|
◆ random() [1/3]
Initializes a new instance of the random class, using a default generated seed value.
◆ random() [2/3]
xtd::random::random |
( |
uint32 |
seed | ) |
|
|
explicit |
Initializes a new instance of the random class, using a specified seed value.
- Parameters
-
seed | A number used to calculate a starting value for the pseudo-random number sequence. |
◆ random() [3/3]
xtd::random::random |
( |
std::random_device & |
random_device | ) |
|
|
explicit |
Initializes a new instance of the random class, using a specified random device value.
- Parameters
-
random_device | A random device value. |
◆ generator()
std::default_random_engine xtd::random::generator |
( |
| ) |
const |
|
noexcept |
Gets the underlying generator.
- Returns
- The underlying generator.
◆ next() [1/6]
virtual int32 xtd::random::next |
( |
| ) |
const |
|
virtual |
◆ next() [2/6]
template<typename value_t >
value_t xtd::random::next |
( |
| ) |
const |
|
inline |
Returns a nonnegative random number.
- Returns
- A value_t greater than or equal to zero and less than std::numeric_limits<value_t>::max()
◆ next() [3/6]
virtual int32 xtd::random::next |
( |
int32 |
max_value | ) |
const |
|
virtual |
Returns a nonnegative random number less than the specified maximum.
- Parameters
-
max_value | The exclusive upper bound of the random number to be generated. max_value must be greater than or equal to zero. |
- Returns
- A 32-bit signed integer greater than or equal to zero and less than max_value
- Exceptions
-
◆ next() [4/6]
template<typename value_t >
value_t xtd::random::next |
( |
value_t |
max_value | ) |
const |
|
inline |
Returns a nonnegative random number less than the specified maximum.
- Parameters
-
max_value | The exclusive upper bound of the random number to be generated. max_value must be greater than or equal to zero. |
- Returns
- A value_t greater than or equal to zero and less than max_value
- Exceptions
-
◆ next() [5/6]
Returns a random number within a specified range.
- Parameters
-
min_value | The inclusive lower bound of the random number returned |
max_value | The exclusive upper bound of the random number returned. max_value must be greater than or equal to min_value. |
- Returns
- A 32-bit signed integer greater than or equal to min_value and less than max_value
- Exceptions
-
◆ next() [6/6]
template<typename value_t >
value_t xtd::random::next |
( |
value_t |
min_value, |
|
|
value_t |
max_value |
|
) |
| const |
|
inline |
Returns a random number within a specified range.
- Parameters
-
min_value | The inclusive lower bound of the random number returned |
max_value | The exclusive upper bound of the random number returned. max_value must be greater than or equal to min_value. |
- Returns
- A value_t greater than or equal to min_value and less than max_value
- Exceptions
-
◆ next_bytes() [1/2]
virtual void xtd::random::next_bytes |
( |
std::vector< xtd::byte > & |
buffer | ) |
const |
|
virtual |
Fills the elements of a specified array of bytes with random numbers.
- Parameters
-
buffer | An array of bytes to contain random numbers. |
- Examples:
- random1.cpp, and random2.cpp.
◆ next_bytes() [2/2]
virtual void xtd::random::next_bytes |
( |
xtd::byte * |
buffer, |
|
|
size_t |
buffer_size |
|
) |
| const |
|
virtual |
Fills the elements of a specified array of bytes with random numbers.
- Parameters
-
buffer | An array of bytes to contain random numbers. |
- Exceptions
-
◆ next_double()
virtual double xtd::random::next_double |
( |
| ) |
const |
|
virtual |
Returns a random number between 0.0 and 1.0.
- Returns
- A double-precision floating point number greater than or equal to 0.0, and less than 1.0.
- Examples:
- random2.cpp.
◆ next_values() [1/2]
template<typename value_t >
void xtd::random::next_values |
( |
std::vector< value_t > & |
buffer | ) |
const |
|
inline |
Fills the elements of a specified array of bytes with random numbers.
- Parameters
-
buffer | An array of bytes to contain random numbers. |
◆ next_values() [2/2]
template<typename value_t >
void xtd::random::next_values |
( |
value_t * |
buffer, |
|
|
size_t |
buffer_size |
|
) |
| const |
|
inline |
Fills the elements of a specified array of bytes with random numbers.
- Parameters
-
buffer | An array of value_t to contain random numbers. |
- Exceptions
-
◆ sample()
virtual double xtd::random::sample |
( |
| ) |
const |
|
protectedvirtual |
Returns a random number between 0.0 and 1.0.
- Returns
- A double-precision floating point number greater than or equal to 0.0, and less than 1.0.
The documentation for this class was generated from the following file: