A generic class for generating pseudorandom numbers in uniform distribution over a specified range.
For example, a Random_source<float> generates float
s, and a Random_source<int> generates int
s. To specify a range, pass the bounds to either of the two constructors, Random_source(result_type, result_type) or Random_source(result_type). Then call Random_source::next() on your Random_source to generate a random number.
There are also two constructors available only for particular result types:
limit
.bool
. It takes a probability and generates true
with that probability and false
otherwise.You can stub your Random_source in order to predetermine the sequence of values that it will return. For details, see Random_source::stub_with(std::initializer_list<result_type>) and Random_source::stub_with(std::vector<result_type>).
Definition at line 215 of file ge211_random.hxx.
Public Types | |
using | result_type = RESULT_TYPE |
The type of value generated by this Random_source. | |
Public Member Functions | |
Random_source (result_type lo, result_type hi) | |
Constructs a random source that generates values between lo and hi , inclusive. More... | |
Random_source (result_type limit) | |
Constructs, for integral result_types only, a random source that generates values between 0 and limit - 1 . More... | |
Random_source (double p_true) | |
Constructs a random source that generates bool s, producing true with probability p_true . More... | |
Random_source (unbounded_type) | |
Constructs a Random_source with no predetermined bounds. More... | |
result_type | next () |
Returns the next random value from this source. More... | |
result_type | operator() () |
Returns the next random value from this source. More... | |
result_type | next_between (result_type lo, result_type hi) |
Returns a random value between lo and hi . More... | |
result_type | operator() (result_type lo, result_type hi) |
Returns a random value between lo and hi . More... | |
void | stub_with (std::initializer_list< result_type > values) |
Configures this Random_source to return a predetermined sequence of values. More... | |
void | stub_with (std::vector< result_type > values) |
Stubs this Random_source using a std::vector. More... | |
void | stub_with (result_type value) |
Stubs this Random_source to always return the given value. More... | |
Random_source (Random_source &&other)=delete | |
Random_sources cannot be move-constructed because they cannot be moved or copied. | |
Random_source & | operator= (Random_source &&other)=delete |
Random_sources cannot be move-assigned because they cannot be moved or copied. | |
Static Public Member Functions | |
static result_type | bound_between (result_type value, result_type lo, result_type hi) |
Given a randomly-generated result_type value , bounds it between lo and hi (inclusive) by adjusting value s less than lo to lo and value s greater than hi to hi . | |
Random_source | ( | result_type | lo, |
result_type | hi | ||
) |
Constructs a random source that generates values between lo
and hi
, inclusive.
Not defined when result_type is bool
. See Random_source<bool>::Random_source(double) instead.
Definition at line 561 of file ge211_random.hxx.
|
explicit |
Constructs, for integral result_types only, a random source that generates values between 0
and limit - 1
.
Thus, Random_source(limit)
is equivalent to Random_source(0, limit - 1)
.
Not defined when result_type is bool
or any non-integral type.
Definition at line 567 of file ge211_random.hxx.
|
explicit |
Constructs a random source that generates bool
s, producing true
with probability p_true
.
Only defined when result_type is bool
.
ge211::Client_logic_error
if p_true
is less than 0.0 or greater than 1.0. Definition at line 573 of file ge211_random.hxx.
|
explicit |
Constructs a Random_source with no predetermined bounds.
Use this with Random_source::operator()(result_type, result_type) or Random_source::next_between(result_type, result_type) to specify the range each time you generate a new number.
Not defined when result_type is bool
.
In this example, we generate one random uppercase letter, one random lowercase letter, and one random digit:
Definition at line 579 of file ge211_random.hxx.
|
inline |
Returns the next random value from this source.
Definition at line 327 of file ge211_random.hxx.
|
inline |
Returns a random value between lo
and hi
.
When using this member function, it is not necessary to initialize your Random_source with bounds, so you should usually construct it using Random_source::Random_source(unbounded_type).
For an example, see Random_source::Random_source(unbounded_type).
If this Random_source is stubbed (e.g., via Random_source::stub_with(std::initializer_list<result_type>)), the result is computed using the static function Random_source::bound_between():
Not defined when result_type is bool
.
Definition at line 384 of file ge211_random.hxx.
|
inline |
Returns the next random value from this source.
(This is an alias for Random_source::next().)
Definition at line 357 of file ge211_random.hxx.
|
inline |
Returns a random value between lo
and hi
.
(This is an alias for Random_source::next_between().)
For an example, see Random_source::Random_source(unbounded_type).
Not defined when result_type is bool
.
Definition at line 397 of file ge211_random.hxx.
void stub_with | ( | result_type | value | ) |
Stubs this Random_source to always return the given value.
If you want to stub multiple values in sequence, see stub_with(std::initializer_list<result_type>) and stub_with(std::vector<result_type>).
Definition at line 599 of file ge211_random.hxx.
void stub_with | ( | std::initializer_list< result_type > | values | ) |
Configures this Random_source to return a predetermined sequence of values.
After passing in a list of values, the Random_source will return those values in order, and then cycle through them repeatedly if necessary.
This is intended for testing, in order to make the values chosen by some component predicable.
Definition at line 592 of file ge211_random.hxx.
void stub_with | ( | std::vector< result_type > | values | ) |
Stubs this Random_source using a std::vector.
After passing in a vector of values, the Random_source will return those values in order, and then cycle through them repeatedly if necessary. This works the same as stub_with(std::initializer_list<result_type>), so you should see that function for an example.
Definition at line 585 of file ge211_random.hxx.