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/console>
#include <xtd/random>
auto main() -> int {
rand.next_bytes(bytes);
for (auto byte_value : bytes)
console::write(
"{, 5}", byte_value);
for (auto ctr = 0; ctr <= 4; ctr++)
for (auto ctr = 0; ctr <= 4; ctr++)
for (auto ctr = 0; ctr <= 4; ctr++)
for (auto ctr = 0; ctr <= 4; ctr++)
for (auto ctr = 0; ctr <= 4; ctr++)
}
Represents the standard input, output, and error streams for console applications.
Definition console.h:36
- Examples
- The following example generates a random integer that it uses as an index to retrieve a string value from an array.
#include <xtd/console>
#include <xtd/random>
auto main() -> int {
auto male_pet_names =
list {
"Rufus",
"Bear",
"Dakota",
"Fido",
"Vanya",
"Samuel",
"Koani",
"Volodya",
"Prince",
"Yiska"};
auto female_pet_names =
list {
"Maggie",
"Penny",
"Saya",
"Princess",
"Abby",
"Laila",
"Sadie",
"Olivia",
"Starlight",
"Talla"};
auto male_index = rnd.next(male_pet_names.size());
auto female_index = rnd.next(female_pet_names.size());
}
- Examples
- as.cpp, change_color.cpp, console_firework.cpp, draw_point.cpp, graph_control.cpp, interlocked.cpp, lock.cpp, lock_guard.cpp, lock_guard_keyword.cpp, minesweeper.cpp, monitor.cpp, random1.cpp, random2.cpp, random3.cpp, and wait_handle.cpp.
|
virtual double | sample () const |
| Returns a random number between 0.0 and 1.0.
|
|
|
| random () |
| Initializes a new instance of the random class, using a default generated seed value.
|
|
| random (uint32 seed) |
| Initializes a new instance of the random class, using a specified seed value.
|
|
| 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.
|
|
template<typename value_t > |
value_t | next () const |
| Returns a nonnegative random number.
|
|
virtual int32 | next (int32 max_value) const |
| Returns a nonnegative random number less than the specified maximum.
|
|
template<typename value_t > |
value_t | next (value_t max_value) const |
| Returns a nonnegative random number less than the specified maximum.
|
|
virtual int32 | next (int32 min_value, int32 max_value) const |
| Returns a random number within a specified range.
|
|
template<typename value_t > |
value_t | next (value_t min_value, value_t max_value) const |
| Returns a random number within a specified range.
|
|
virtual void | next_bytes (std::vector< xtd::byte > &buffer) const |
| Fills the elements of a specified array of bytes with random numbers.
|
|
virtual void | next_bytes (xtd::byte *buffer, size_t buffer_size) const |
| Fills the elements of a specified array of bytes with random numbers.
|
|
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.
|
|
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.
|
|
virtual double | next_double () const |
| Returns a random number between 0.0 and 1.0.
|
|
|
| object ()=default |
| Create a new instance of the ultimate base class object.
|
|
virtual bool | equals (const object &obj) const noexcept |
| Determines whether the specified object is equal to the current object.
|
|
virtual size_t | get_hash_code () const noexcept |
| Serves as a hash function for a particular type.
|
|
virtual type_object | get_type () const noexcept |
| Gets the type of the current instance.
|
|
template<typename object_t > |
xtd::uptr< object_t > | memberwise_clone () const |
| Creates a shallow copy of the current object.
|
|
virtual xtd::string | to_string () const noexcept |
| Returns a xtd::string that represents the current object.
|
|
template<typename object_a_t , typename object_b_t > |
static bool | equals (const object_a_t &object_a, const object_b_t &object_b) noexcept |
| Determines whether the specified object instances are considered equal.
|
|
template<typename object_a_t , typename object_b_t > |
static bool | reference_equals (const object_a_t &object_a, const object_b_t &object_b) noexcept |
| Determines whether the specified object instances are the same instance.
|
|
◆ 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.
◆ 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_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
-
◆ 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.
◆ 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: