|
tgen
|
Operations for basic number generation and basic random operations. More...
Classes | |
| struct | tgen::print |
| Printer helper for standard types. More... | |
| struct | tgen::println |
| Printer helper for standard types, printing on a new line. More... | |
| struct | tgen::print_cols< Args > |
| Printer helper for printing containers or sequential generator elements as columns. More... | |
| struct | tgen::weighted_sampler< T > |
| Sampler for repeated draws from a fixed weighted distribution. More... | |
| struct | tgen::distinct_range< T > |
| Distinct generator for integral ranges. More... | |
| struct | tgen::distinct_container< T > |
| Distinct generator for containers. More... | |
| struct | tgen::distinct< Func, Args > |
| Distinct generator for discrete uniform functions. More... | |
| struct | tgen::gen_base< Gen > |
| Base class for generators (should not be instantiated). More... | |
| struct | tgen::gen_value_base< Val > |
| Base class for generator values (should not be instantiated). More... | |
Macros | |
| #define | tgen_ensure(cond, ...) |
| Ensures condition is true. | |
Functions | |
| template<typename T> | |
| T | tgen::next (T right) |
| Returns a random number smaller than value. | |
| template<typename T> | |
| T | tgen::next (T left, T right) |
| Returns a random number in range. | |
| template<typename T> | |
| T | tgen::wnext (T right, int w) |
| Returns a skewed random number smaller than value. | |
| template<typename T> | |
| T | tgen::wnext (T left, T right, int w) |
| Returns a skewed random number in range. | |
| template<typename T> | |
| size_t | tgen::next_by_distribution (const std::vector< T > &distribution) |
| Returns random index with given probabilities. | |
| template<typename T> | |
| std::vector< int > | tgen::many_by_distribution (int k, const std::vector< T > &distribution) |
| Returns many random indices with given probabilities. | |
| template<typename It> | |
| void | tgen::shuffle (It first, It last) |
| Shuffles range inplace, for random_access_iterator. | |
| template<typename C> | |
| auto | tgen::shuffled (const C &container) |
| Shuffles a container. | |
| template<typename It> | |
| It::value_type | tgen::pick (It first, It last) |
| Chooses a random element from an iterator range. | |
| template<typename C> | |
| C::value_type | tgen::pick (const C &container) |
| Chooses a random element from container. | |
| template<typename C, typename T> | |
| C::value_type | tgen::pick_by_distribution (const C &container, std::vector< T > distribution) |
| Chooses a random element with given probabilities. | |
| template<typename C> | |
| C | tgen::choose (const C &container, int k) |
| Chooses elements from container, as in a subsequence fixed length. | |
Operations for basic number generation and basic random operations.
| #define tgen_ensure | ( | cond, | |
| ... ) |
Ensures condition is true.
| cond | Condition to be ensured. |
| ... | Empty or a string with an error message. |
| std::runtime_error | if cond is not true. |
Chooses elements from container, as in a subsequence fixed length.
| container | Container to choose from. |
| k | Number of elements to be chosen. |
Also works with std::initializer_list.
O(|container|).
| std::vector< int > tgen::many_by_distribution | ( | int | k, |
| const std::vector< T > & | distribution ) |
Returns many random indices with given probabilities.
| k | Number of indices to be generated. |
| distribution | Probability distribution. |
Internally builds a tgen::weighted_sampler and calls weighted_sampler::next k times.
O(k + |distribution|).
| T tgen::next | ( | T | left, |
| T | right ) |
Returns a random number in range.
| T | type to be generated. Should be an arithmetic type: int, double, long long, char, etc. |
| left | Left endpoint of the range. |
| right | Right endpoint of the range. |
O(1).
| T tgen::next | ( | T | right | ) |
Returns a random number smaller than value.
| T | type to be generated. Should be an arithmetic type: int, double, long long, char, etc. |
| right | Endpoint of the range. |
O(1).
| size_t tgen::next_by_distribution | ( | const std::vector< T > & | distribution | ) |
Returns random index with given probabilities.
| distribution | Probability distribution. |
If many indices are needed from the same distribution, prefer many_by_distribution, or build a tgen::weighted_sampler once and call weighted_sampler::next repeatedly.
O(|distribution|).
Chooses a random element from container.
| container | Container to choose from. |
Also works with std::initializer_list.
O(1) for random_access_iterator, O(|container|) otherwise.
| It::value_type tgen::pick | ( | It | first, |
| It | last ) |
Chooses a random element from an iterator range.
| first | First iterator of range. |
| last | First iterator outside of range. |
O(1) for random_access_iterator, O(|last - first|) otherwise.
| C::value_type tgen::pick_by_distribution | ( | const C & | container, |
| std::vector< T > | distribution ) |
Chooses a random element with given probabilities.
| container | Container to choose from. |
| distribution | Probability distribution. |
Also works with std::initializer_list.
O(1) for random_access_iterator, O(|container|) otherwise.
| void tgen::shuffle | ( | It | first, |
| It | last ) |
Shuffles range inplace, for random_access_iterator.
| first | First iterator of range. |
| last | First iterator outside of range. |
Shuffles [first, last) uniformly inplace. Works for std::vector, std::string, etc, but not std::set. For those, use tgen::shuffled(container).
O(|last - first|).
Shuffles a container.
| container | Container to be shuffled. |
Works for containers and std::initializer_list. If container is ordered (std::set, etc) or std::initializer_list, returns a std::vector (implemented in a different function).
O(|container|).
| T tgen::wnext | ( | T | left, |
| T | right, | ||
| int | w ) |
Returns a skewed random number in range.
| T | Arithmetic type, same as for tgen::next. |
| left | Left endpoint of the range. |
| right | Right endpoint of the range. |
| w | Skew parameter. |
See tgen::wnext for more details.
O(|w|).
| T tgen::wnext | ( | T | right, |
| int | w ) |
Returns a skewed random number smaller than value.
| T | Arithmetic type, same as for tgen::next. |
| right | Right endpoint (half-open range), same convention as tgen::next(right). |
| w | Skew parameter. |
The continuous version corresponds to Beta distributions:
w = 0 is uniform.
Positive w means taking the maximum of w
uniform samples.
Negative w means taking the minimum of -w
uniform samples.
O(|w|).