Construct version 5.4.4
An agent based modeling framework
|
Manages all random number generation in Construct. More...
Public Member Functions | |
void | set_seed (unsigned int seed) noexcept |
float | uniform () noexcept |
Generates a U(0,1) as a float. More... | |
float | uniform (float min, float max) noexcept |
Generates a U(min,max) as a float. More... | |
unsigned int | integer (unsigned int max) |
Generates a random integer ∈[0,max). More... | |
unsigned int | poisson_number (float lambda) |
Generates a random integer from a poisson distribution. More... | |
bool | randombool (float probability=.5) |
Generates a random boolean. More... | |
float | normal (float mean, float standard_deviation) |
Generates a random value from a normal distribution. More... | |
float | exponential (float mean) |
Generates a random value from an exponential distribution. More... | |
template<typename T > | |
void | vector_shuffle (std::vector< T > &A) noexcept |
Shuffles a vector so that each element is moved to another random element. More... | |
template<typename T > | |
T & | select (std::vector< T > &vec) |
Selects a random element from a vector. More... | |
unsigned int | find_dist_index (std::vector< float > &pdf) |
An element index is selected based on the submitted probability distribution function vector. More... | |
std::vector< unsigned int > | order_by_pdf (std::vector< float > &pdf) |
Creates a vector of unique indexes ordered based on the submitted probability distribution function. More... | |
Public Attributes | |
unsigned int | seed = static_cast<unsigned int>(time(nullptr)) |
Manages all random number generation in Construct.
The central location for all random functions. No model or Manager should generate random numbers independently of this class.
float Random::exponential | ( | float | mean | ) |
Generates a random value from an exponential distribution.
Uses the standard library exponential_distribution
mean | The mean of the distribution. |
Example
Output:
exponential_distribution(3.5) : 0.0 - 0.1 : ***************************** 0.1 - 0.2 : ********************* 0.2 - 0.3 : ************** 0.3 - 0.4 : ********* 0.4 - 0.5 : ******* 0.5 - 0.6 : ***** 0.6 - 0.7 : *** 0.7 - 0.8 : ** 0.8 - 0.9 : * 0.9 - 1.0 : *
Complexity
Constant
Exception Safety
If a negative value is given for the mean, an assertion is raised.
unsigned int Random::find_dist_index | ( | std::vector< float > & | ) |
An element index is selected based on the submitted probability distribution function vector.
Uses the standard library discrete_distribution
The probability distribution from which sampling will take place. |
Example
Output:
generating random indexes based on custom cdf 2 1 0 2 2 2 2 0 2 2
Complexity
Linear is size of the submitted pdf.
Iterator validity
No Changes.
Exception Safety
An assertion is raised if any values in pdf are less than zero or if all elements are zero.
unsigned int Random::integer | ( | unsigned int | max | ) |
Generates a random integer ∈[0,max).
max | The largest but non-inclusive value in the range of the distribution. |
Example
Output:
some random numbers between 0 and 10 0 7 3 1 3 1 8 9 2 9
Complexity
Constant
Exception Safety
An assertion is raised if max is equal to zero.
float Random::normal | ( | float | mean, |
float | standard_deviation | ||
) |
Generates a random value from a normal distribution.
Uses the standard library normal_distribution
mean | The mean of the distribution. |
standard_deviation | The standard_deviation of the distribution. |
Example
Output:
normal_distribution (5.0,2.0): 0 - 1 : * 1 - 2 : **** 2 - 3 : ********* 3 - 4 : *************** 4 - 5 : ****************** 5 - 6 : ******************* 6 - 7 : *************** 7 - 8 : ******** 8 - 9 : **** 9 - 10 : *
Complexity
Constant
Exception Safety
If a negative value is given for standard_deviation, an assertion is raised.
std::vector< unsigned int > Random::order_by_pdf | ( | std::vector< float > & | ) |
Creates a vector of unique indexes ordered based on the submitted probability distribution function.
The first element of the vector is selected based on the unmodified pdf. After an index is saved, that element in the pdf is removed and the next element is slected based on the new pdf. This continues until no elements are left in the pdf.
If pdf elements are zero, those elements are automatically removed before the ordered vector is created.
The probability distribution for which the unique vector of indexes is ordered upon. |
Example
Output:
Probabilistically ordered indexes are 1 4 0 2 3
Complexity
Number of non-zero elements in the pdf squared.
Iterator validity
No changes
Exception Safety
An assertion is raised if any values in pdf are less than zero or if all elements are zero.
unsigned int Random::poisson_number | ( | float | lambda | ) |
Generates a random integer from a poisson distribution.
Uses the standard library poisson_distribution
lambda | The mean of the distribution. |
Example
Output:
poisson_distribution (mean=4.1): 0 : * 1 : ****** 2 : ************* 3 : ******************* 4 : ******************* 5 : *************** 6 : *********** 7 : ****** 8 : *** 9 : *
Complexity
Constant
Exception Safety
If a negative value is given for the mean, an assertion is raised.
bool Random::randombool | ( | float | probability = .5 | ) |
Generates a random boolean.
probability | The probability the boolean is true. Default value is 0.5. |
Example
Output:
some random numbers between 0 and 10 0 0 1 1 1 1 1 1 1 0
Complexity
Constant
Exception Safety
If a negative value is given for the probability, an assertion is raised.
|
inline |
Selects a random element from a vector.
vec | Vector of values. |
Example
Output:
random elements of vector 7 0 5 2 1 5 2 3 4 1
Complexity
Constant
Iterator validity
No Changes.
Exception Safety
An assertion is raised if the vector size is zero.
|
noexcept |
Generates a U(0,1) as a float.
Uses the standard library uniform_real_distribution
Example
Output:
uniform distribution 0.0 - 0.1 : ********* 0.1 - 0.2 : ********* 0.2 - 0.3 : ********* 0.3 - 0.4 : ********* 0.4 - 0.5 : ********* 0.5 - 0.6 : ********* 0.6 - 0.7 : ********* 0.7 - 0.8 : ********* 0.8 - 0.9 : ********* 0.9 - 1.0 : *********
Complexity
Constant.
Exception Safety
Strong Guarantee: This function never throws an exception.
|
noexcept |
Generates a U(min,max) as a float.
min | The smallest value in the range of the uniform distribution. |
max | The largest but non-inclusive value in the range of the uniform distribution. |
Example
Output:
some random numbers between -5 and 10 -4.11045 6.32127 7.68560 9.96905 0.62124 3.94087 5.23497 4.11221 2.00376 2.58868
Complexity
Constant
Exception Safety
Strong Guarantee: This function never throws an exception.
|
inlinenoexcept |
Shuffles a vector so that each element is moved to another random element.
A non-zero chance exists that the element does not change position.
A | Vector of elements that are to be shuffled. |
Example
Output:
shuffled list of numbers 6 9 4 2 3 1 7 8 0 5
Complexity
Linear in size of vector.
Iterator validity
Iterators remain valid, however the elements they point to have been modified.
Exception Safety
Strong Guarantee: This function never throws an exception.