Operations for basic number generation and basic random operations.
More...
|
| #define | tgen_ensure(cond, ...) |
| | Ensures condition is true.
|
|
| template<typename T> |
| T | tgen::next (T n) |
| | Returns a random number up to value.
|
| template<typename T> |
| T | tgen::next (T l, T r) |
| | Returns a 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 It> |
| void | tgen::shuffle (It first, It last) |
| | Shuffles range inplace, for random_access_iterator.
|
| template<typename C, std::enable_if_t<!is_associative_container< C >::value and !is_generator_instance< C >::value, int > = 0> |
| C | tgen::shuffled (const C &container) |
| | Shuffles a container.
|
| template<typename It> |
| It::value_type | tgen::any (It first, It last) |
| | Choses a random element from iterator range.
|
| template<typename C, std::enable_if_t<!is_generator_instance< C >::value, int > = 0> |
| C::value_type | tgen::any (const C &container) |
| | Choses a random element from container.
|
| template<typename C, typename T, std::enable_if_t<!is_generator_instance< C >::value, int > = 0> |
| C::value_type | tgen::any_by_distribution (const C &container, std::vector< T > distribution) |
| | Choses a random element with given probabilities.
|
| template<typename C, std::enable_if_t<!is_generator_instance< C >::value, int > = 0> |
| C | tgen::choose (int k, const C &container) |
| | Chooses elements from container, as in a subsequence fixed length.
|
Operations for basic number generation and basic random operations.
Examples
std::vector<int> v(size);
for (
int& i : v) i =
tgen::any({5, 10, 15, 20});
void shuffle(It first, It last)
Shuffles range inplace, for random_access_iterator.
T next(T n)
Returns a random number up to value.
It::value_type any(It first, It last)
Choses a random element from iterator range.
#define tgen_ensure(cond,...)
Ensures condition is true.
C choose(int k, const C &container)
Chooses elements from container, as in a subsequence fixed length.
◆ tgen_ensure
| #define tgen_ensure |
( |
| cond, |
|
|
| ... ) |
Ensures condition is true.
- Parameters
-
| cond | Condition to me ensured. |
| ... | Empty or a string with an error message. |
- Exceptions
-
| std::runtime_error | if cond is not true. |
Examples
Definition at line 95 of file tgen.h.
◆ any() [1/2]
| C::value_type tgen::any |
( |
const C & | container | ) |
|
Choses a random element from container.
- Parameters
-
| container | Container to choose from. |
Also works with std::initializer_list.
- Returns
- A uniformly random element from container.
Complexity
O(1) for random_access_iterator, O(|container|) otherwise.
Examples
std::vector<int> v = {1, 2, 3, 4, 5};
Definition at line 635 of file tgen.h.
◆ any() [2/2]
template<typename It>
| It::value_type tgen::any |
( |
It | first, |
|
|
It | last ) |
Choses a random element from iterator range.
- Parameters
-
| first | First iterator of range. |
| last | First iterator outside of range. |
- Returns
- A uniformly random element from the range [first, last).
Complexity
O(1) for random_access_iterator, O(|last - first|) otherwise.
Examples
std::set<int> st = {1, 2, 3, 4, 5};
Definition at line 624 of file tgen.h.
◆ any_by_distribution()
| C::value_type tgen::any_by_distribution |
( |
const C & | container, |
|
|
std::vector< T > | distribution ) |
Choses a random element with given probabilities.
- Parameters
-
| container | Container to choose from. |
| distribution | Probability distribution. |
Also works with std::initializer_list.
- Returns
- For i in [0, container.size()-1], container[i] with probability proportional to distribution[i].
Complexity
O(1) for random_access_iterator, O(|container|) otherwise.
Examples
C::value_type any_by_distribution(const C &container, std::vector< T > distribution)
Choses a random element with given probabilities.
Definition at line 653 of file tgen.h.
◆ choose()
| C tgen::choose |
( |
int | k, |
|
|
const C & | container ) |
Chooses elements from container, as in a subsequence fixed length.
- Parameters
-
| k | Number of elements to be chosen. |
| container | Container to choose from. |
Also works with std::initializer_list.
- Returns
- A uniformly random subsequence of length k.
Complexity
O(|container|).
Examples
std::vector<int> v = {1, 2, 3, 4, 5};
Definition at line 702 of file tgen.h.
◆ next() [1/2]
template<typename T>
| T tgen::next |
( |
T | l, |
|
|
T | r ) |
Returns a random number in range.
- Template Parameters
-
| T | type to be generated. Should be an arithmetic type: int, double, long long, char, etc. |
- Parameters
-
| l | Left endpoint of the range. |
| r | Light endpoint of the range. |
- Returns
- A random number in [l, r], chosen uniformly.
Complexity
O(1).
Examples
Definition at line 543 of file tgen.h.
◆ next() [2/2]
Returns a random number up to value.
- Template Parameters
-
| T | type to be generated. Should be an arithmetic type: int, double, long long, char, etc. |
- Parameters
-
- Returns
- A random number in [0, n), chosen uniformly.
Complexity
O(1).
Examples
Definition at line 528 of file tgen.h.
◆ next_by_distribution()
template<typename T>
| size_t tgen::next_by_distribution |
( |
const std::vector< T > & | distribution | ) |
|
Returns random index with given probabilities.
- Parameters
-
| distribution | Probability distribution. |
- Returns
- i in [0, distribution.size()-] with probability proportional to distribution[i].
Complexity
O(|distribution|).
Examples
size_t next_by_distribution(const std::vector< T > &distribution)
Returns random index with given probabilities.
Definition at line 557 of file tgen.h.
◆ shuffle()
template<typename It>
| void tgen::shuffle |
( |
It | first, |
|
|
It | last ) |
Shuffles range inplace, for random_access_iterator.
- Parameters
-
| 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).
Complexity
O(|last - first|).
Examples
std::vector<int> v = {1, 2, 3, 4, 5};
Definition at line 576 of file tgen.h.
◆ shuffled()
| C tgen::shuffled |
( |
const C & | container | ) |
|
|
nodiscard |
Shuffles a container.
- Parameters
-
| 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).
- Returns
- The shuffled container.
Complexity
O(|container|).
Examples
std::vector<int> st = {1, 2, 3, 4, 5};
C shuffled(const C &container)
Shuffles a container.
Definition at line 597 of file tgen.h.