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 l, T r) |
| | Returns a random number in [l, r].
|
| template<typename It> |
| void | tgen::shuffle (It first, It last) |
| | Shuffles range inplace, for random_access_iterator.
|
| template<typename C, std::enable_if_t<!_detail::is_associative_container< C >::value and !_detail::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<!_detail::is_generator_instance< C >::value, int > = 0> |
| C::value_type | tgen::any (const C &container) |
| | Choses a random element from container.
|
| template<typename C, std::enable_if_t<!_detail::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.
It::value_type any(It first, It last)
Choses a random element from iterator range.
T next(T l, T r)
Returns a random number in [l, r].
#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 87 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 370 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 359 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 390 of file tgen.h.
◆ next()
template<typename T>
| T tgen::next |
( |
T | l, |
|
|
T | r ) |
Returns a random number in [l, r].
- 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 302 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 316 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 330 of file tgen.h.