xtd 0.2.0
Loading...
Searching...
No Matches
xtd::random Class Reference
Inheritance diagram for xtd::random:
xtd::object

Definition

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
Remarks
Pseudo-random numbers are chosen with equal probability from a finite set of numbers. The chosen numbers are not completely random because a definite mathematical algorithm is used to select them, but they are sufficiently random for practical purposes. The current implementation of the random class is based on Donald E. Knuth's subtractive random number generator algorithm. For more information, see D. E. Knuth. "The Art of Computer Programming, volume 2: Seminumerical Algorithms". Addison-Wesley, Reading, MA, second edition, 1981.
The random number generation starts from a seed value. If the same seed is used repeatedly, the same series of numbers is generated. One way to produce different sequences is to make the seed value time-dependent, thereby producing a different series with each new instance of random. By default, the parameterless constructor of the random class uses the system clock to generate its seed value, while its parameterized constructor can take an int32 value based on the number of ticks in the current time. However, because the clock has finite resolution, using the parameterless constructor to create different random objects in close succession creates random number generators that produce identical sequences of random numbers. The following example illustrates that two random objects that are instantiated in close succession generate an identical series of random numbers.
#include <xtd/console>
#include <xtd/random>
using namespace xtd;
using namespace xtd::collections::generic;
auto main() -> int {
auto bytes1 = list<unsigned char>(100);
auto bytes2 = list<unsigned char>(100);
auto rnd1 = xtd::random {};
auto rnd2 = xtd::random {};
rnd1.next_bytes(bytes1);
rnd2.next_bytes(bytes2);
console::write_line("First Series:");
for (auto i = 0ul; i < bytes1.size(); i++) {
console::write("{, 5}", bytes1[i]);
if ((i + 1) % 10 == 0)
}
console::write_line("Second Series:");
for (auto i = 0ul; i < bytes2.size(); i++) {
console::write("{, 5}", bytes2[i]);
if ((i + 1) % 10 == 0)
}
}
// This code can produce the following output :
//
// First Series:
// 104 222 250 126 113 23 137 65 199 68
// 72 242 71 179 29 70 1 160 135 214
// 129 158 46 16 137 48 66 28 81 190
// 157 155 212 222 3 211 14 137 1 30
// 50 217 229 181 252 55 241 74 230 148
// 153 59 16 152 54 226 11 64 133 236
// 90 255 30 52 45 116 203 193 27 167
// 187 190 68 218 37 16 229 116 150 161
// 183 130 219 90 192 254 24 232 174 116
// 46 191 226 53 118 198 148 29 76 106
//
// Second Series:
// 104 222 250 126 113 23 137 65 199 68
// 72 242 71 179 29 70 1 160 135 214
// 129 158 46 16 137 48 66 28 81 190
// 157 155 212 222 3 211 14 137 1 30
// 50 217 229 181 252 55 241 74 230 148
// 153 59 16 152 54 226 11 64 133 236
// 90 255 30 52 45 116 203 193 27 167
// 187 190 68 218 37 16 229 116 150 161
// 183 130 219 90 192 254 24 232 174 116
// 46 191 226 53 118 198 148 29 76 106
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:71
static void write(arg_t &&value)
Writes the text representation of the specified value to the standard output stream.
Definition console.hpp:462
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
Represents a pseudo-random number generator, a device that produces a sequence of numbers that meet c...
Definition random.hpp:40
virtual void next_bytes(std::vector< xtd::byte > &buffer) const
Fills the elements of a specified array of bytes with random numbers.
@ i
The I key.
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:15
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10
This problem can be avoided by creating a single random object rather than multiple ones.
To improve performance, create one random object to generate many random numbers over time, instead of repeatedly creating a new random objects to generate one random number.
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>
using namespace xtd;
using namespace xtd::collections::generic;
auto main() -> int {
// Instantiate random number generator using system-supplied value as seed.
auto rand = xtd::random {};
// Generate and display 5 random byte (integer) values.
auto bytes = list<unsigned char>(5);
rand.next_bytes(bytes);
console::write_line("Five random byte values:");
for (auto byte_value : bytes)
console::write("{, 5}", byte_value);
// Generate and display 5 random integers.
console::write_line("Five random integer values:");
for (auto ctr = 0; ctr <= 4; ctr++)
console::write("{, 15}", rand.next());
// Generate and display 5 random integers between 0 and 100.//
console::write_line("Five random integers between 0 and 100:");
for (auto ctr = 0; ctr <= 4; ctr++)
console::write("{, 10}", rand.next(101));
// Generate and display 5 random integers from 50 to 100.
console::write_line("Five random integers between 50 and 100:");
for (auto ctr = 0; ctr <= 4; ctr++)
console::write("{, 10}", rand.next(50, 101));
// Generate and display 5 random floating point values from 0 to 1.
console::write_line("Five Doubles:");
for (auto ctr = 0; ctr <= 4; ctr++)
console::write("{, 10}", rand.next_double());
// Generate and display 5 random floating point values from 0 to 5.
console::write_line("Five Doubles between 0 and 5:");
for (auto ctr = 0; ctr <= 4; ctr++)
console::write("{, 10}", rand.next_double() * 5);
}
// This code can produce the following output :
//
// Five random byte values:
// 150 243 92 141 0
// Five random integer values:
// 4.14343E+08 2.14086E+09 1.73486E+09 1.88133E+09 1.69685E+09
// Five random integers between 0 and 100:
// 35 86 86 68 10
// Five random integers between 50 and 100:
// 76 57 66 97 56
// Five Doubles:
// 0.211096 0.172007 0.507414 0.48576 0.754658
// Five Doubles between 0 and 5:
// 1.40691 2.7078 3.87732 1.0691 0.478343
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>
using namespace xtd;
using namespace xtd::collections::generic;
auto main() -> int {
auto rnd = xtd::random {};
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"};
// Generate random indexes for pet names.
auto male_index = rnd.next(male_pet_names.size());
auto female_index = rnd.next(female_pet_names.size());
// Display the result.
console::write_line("Suggested pet name of the day: ");
console::write_line(" For a male: {0}", male_pet_names[male_index]);
console::write_line(" For a female: {0}", female_pet_names[female_index]);
}
// This code can produce the following output :
//
// Suggested pet name of the day:
// For a male: Prince
// For a female: Talla
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.

Protected Member Functions

virtual double sample () const
 Returns a random number between 0.0 and 1.0.
 

Public Constructors

 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.
 

Public Properties

std::default_random_engine generator () const noexcept
 Gets the underlying generator.
 

Public Methods

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.
 

Additional Inherited Members

- Public Member Functions inherited from xtd::object
 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.
 
- Static Public Member Functions inherited from xtd::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.
 

Constructor & Destructor Documentation

◆ random() [1/3]

xtd::random::random ( )

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
seedA 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_deviceA random device value.

Member Function Documentation

◆ 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

Returns a nonnegative random number.

Returns
A 32-bit signed integer greater than or equal to zero and less than std::numeric_limits<int32>::max())
Examples
draw_point.cpp, lock.cpp, lock_guard_keyword.cpp, and minesweeper.cpp.

◆ 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_valueThe 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
argument_out_of_range_exceptionmax_value is less than zero.
Remarks
The next(int32) overload returns random integers that range from 0 to max_value – 1. However, if max_value is 0, the method returns 0.

◆ 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_valueThe 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
argument_out_of_range_exceptionmax_value is less than zero.
Remarks
The next(value_t) overload returns random integers that range from 0 to max_value – 1. However, if max_value is 0, the method returns 0.

◆ next() [5/6]

virtual int32 xtd::random::next ( int32  min_value,
int32  max_value 
) const
virtual

Returns a random number within a specified range.

Parameters
min_valueThe inclusive lower bound of the random number returned
max_valueThe 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
argument_out_of_range_exceptionmin_value is greater than max_value.
Remarks
The next(int32, int32) overload returns random integers that range from min_value to max_value – 1. However, if max_value equals min_value, the method returns min_value.
Unlike the other overloads of the next method, which return only non-negative values, this method can return a negative random integer.

◆ 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_valueThe inclusive lower bound of the random number returned
max_valueThe 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
argument_out_of_range_exceptionmin_value is greater than max_value.
Remarks
The next(value_t, value_t) overload returns random integers that range from min_value to max_value – 1. However, if max_value equals min_value, the method returns min_value.
Unlike the other overloads of the next method, which return only non-negative values, this method can return a negative random integer.

◆ 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
bufferAn array of bytes to contain random numbers.
Remarks
Each element of the array of bytes is set to a random number greater than or equal to zero, and less than or equal to std::numeric_limits<xtd::byte>::max().
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
bufferAn array of bytes to contain random numbers.
Exceptions
argument_null_exceptionbuffer is null.
Remarks
Each element of the array of bytes is set to a random number greater than or equal to zero, and less than or equal to std::numeric_limits<xtd::byte>::max().

◆ 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
bufferAn array of bytes to contain random numbers.
Remarks
Each element of the array of bytes is set to a random number greater than or equal to zero, and less than or equal to std::numeric_limits<value_t>::max().

◆ 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
bufferAn array of value_t to contain random numbers.
Exceptions
argument_null_exceptionbuffer is null.
Remarks
Each element of the array of values is set to a random number greater than or equal to zero, and less than or equal to std::numeric_limits<value_t>::max().

◆ 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.
Remarks
This method is the public version of the protected method, sample

◆ 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.
Remarks
To produce a different random distribution or a different random number generator principle, derive a class from the random class and virtual the sample method.

The documentation for this class was generated from the following file: