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.hpp: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 =
array {
"Rufus",
"Bear",
"Dakota",
"Fido",
"Vanya",
"Samuel",
"Koani",
"Volodya",
"Prince",
"Yiska"};
auto female_pet_names =
array {
"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.
|
template<class value_t > |
xtd::array< value_t > | get_items (const xtd::read_only_span< value_t > &choices, xtd::size length) |
| Creates an array populated with items chosen at random from the provided set of choices.
|
|
template<class value_t > |
xtd::array< value_t > | get_items (const xtd::array< value_t > &choices, xtd::size length) |
| Creates an array populated with items chosen at random from the provided set of choices.
|
|
template<class value_t > |
void | get_items (const xtd::read_only_span< value_t > &choices, xtd::span< value_t > &destination) |
| Fills the elements of a specified span with items chosen at random from the provided set of choices.
|
|
virtual int32 | next () const |
| Returns a nonnegative random number.
|
|
template<class 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<class 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<class 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 (xtd::span< xtd::byte > &buffer) const |
| Fills the elements of a specified array of bytes with random numbers.
|
|
virtual void | next_bytes (xtd::array< xtd::byte > &buffer) 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.
|
|
template<class value_t > |
void | next_values (xtd::span< value_t > &buffer) const |
| Fills the elements of a specified xtd::span of bytes with random numbers.
|
|
template<class value_t > |
void | next_values (xtd::array< value_t > &buffer) const |
| Fills the elements of a specified array of bytes with random numbers.
|
|
virtual int64 | next_int64 () const |
| Returns a nonnegative random number.
|
|
virtual int64 | next_int64 (int64 max_value) const |
| Returns a nonnegative random number less than the specified maximum.
|
|
virtual int64 | next_int64 (int64 min_value, int64 max_value) const |
| Returns a random number within a specified range.
|
|
virtual single | next_single () const |
| Returns a random number between 0.0 and 1.0.
|
|
template<class value_t > |
void | shuffle (xtd::span< value_t > &values) const |
| Performs an in-place shuffle of a span.
|
|
template<class collection_t > |
void | shuffle (collection_t &values) const |
| Performs an in-place shuffle of an array.
|
|
|
| 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 xtd::size | 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<class 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<class object_a_t , class 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<class object_a_t , class 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.
|
|