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_t, int32_t), 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;
int main() {
vector<uint8_t> bytes(5);
for (uint8_t 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++)
}
virtual double next_double() const
Returns a random number between 0.0 and 1.0.
Definition: random.h:136
virtual int32_t next() const
Returns a nonnegative random number.
Definition: random.h:52
- 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;
int main() {
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());
}
|
| random () |
| Initializes a new instance of the random class, using a default generated seed value.
|
|
| random (std::random_device &random_device) |
| Initializes a new instance of the random class, using a specified random device value.
|
|
| random (uint32_t seed) |
| Initializes a new instance of the random class, using a specified seed value.
|
|
virtual int32_t | next () const |
| Returns a nonnegative random number.
|
|
template<typename value_t > |
value_t | next () const |
| Returns a nonnegative random number.
|
|
virtual int32_t | next (int32_t max_value) const |
| Returns a nonnegative random number less than the specified maximum.
|
|
virtual int32_t | next (int32_t min_value, int32_t max_value) const |
| Returns a random number within a specified range.
|
|
template<typename value_t > |
value_t | next (value_t max_value) const |
| Returns a nonnegative random number less than the specified maximum.
|
|
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< uint8_t > &buffer) const |
| Fills the elements of a specified array of bytes with random numbers.
|
|
virtual void | next_bytes (uint8_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.
|
|
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.
|
|
| 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.
|
|
template<typename object_t > |
std::unique_ptr< object_t > | memberwise_clone () const |
| Gets the type of the current instance.
|
|
virtual xtd::ustring | to_string () const noexcept |
| Returns a std::string that represents the current object.
|
|