tgen
Loading...
Searching...
No Matches

Distinct generator for integral ranges. More...

Public Member Functions

 distinct_range (T left, T right)
 Creates distinct generator for values in given range.
size () const
 Returns the number of values left to generate.
gen ()
 Generates a distinct random value in the defined range.
auto gen_list (int count)
 Generates a list of several distinct values.
auto gen_all ()
 Generates all distinct values left to generate.

Detailed Description

template<typename T>
struct tgen::distinct_range< T >

Distinct generator for integral ranges.

Examples

// Prints 3 distinct numbers from 1 to 10, uniformly at random.
std::cout << u.gen() << " " << u.gen() << " " << u.gen() << std::endl;
Distinct generator for integral ranges.
Definition tgen.h:1048

Definition at line 1048 of file tgen.h.

Constructor & Destructor Documentation

◆ distinct_range()

template<typename T>
tgen::distinct_range< T >::distinct_range ( T left,
T right )
inline

Creates distinct generator for values in given range.

Parameters
leftLeft endpoint of the range.
rightRight endpoint of the range.

The generator will generate values in [l, r].

Examples

// Creates a generator for distinct nubers from 5 to 10.
auto u = tgen::distinct_range(5, 10);

Definition at line 1057 of file tgen.h.

Member Function Documentation

◆ gen()

template<typename T>
T tgen::distinct_range< T >::gen ( )
inline

Generates a distinct random value in the defined range.

Returns
A value in the given range that has not been generated before, uniformly at random.
Exceptions
std::runtime_errorif there no more values to generate.

Complexity

O(log(n)) for the n-th call of gen.

Examples

// Prints 3 distinct numbers from 1 to 10, uniformly at random.
std::cout << u.gen() << " " << u.gen() << " " << u.gen() << std::endl;

Definition at line 1066 of file tgen.h.

◆ gen_all()

template<typename T>
auto tgen::distinct_range< T >::gen_all ( )
inline

Generates all distinct values left to generate.

Returns
A uniformly random tgen::list::value with all distinct values from the range that have not been generated yet.

Complexity

The same as calling distinct_range::gen until there are no more distinct values.

Examples

// Prints all numbers from 1 to 10 in a uniformly random order.
std::cout << tgen::distinct_range(1, 10).gen_all() << std::endl;
auto gen_all()
Generates all distinct values left to generate.
Definition tgen.h:1112

Definition at line 1112 of file tgen.h.

◆ gen_list()

template<typename T>
auto tgen::distinct_range< T >::gen_list ( int size)
inline

Generates a list of several distinct values.

Returns
A uniformly random tgen::list::value with size distinct values from the range.
Exceptions
std::runtime_errorif there are not enough values to generate.

Complexity

The same as size calls to distinct_range::gen.

Examples

// Prints 3 distinct numbers from 1 to 10, uniformly at random.
std::cout << tgen::distinct_range(1, 10).gen_list(3) << std::endl;
auto gen_list(int count)
Generates a list of several distinct values.
Definition tgen.h:1087

Definition at line 1087 of file tgen.h.

◆ size()

template<typename T>
T tgen::distinct_range< T >::size ( ) const
inline

Returns the number of values left to generate.

Returns
The number of values left to generate.

Complexity

O(1).

Examples

// Prints current size of distinct generator.
u.gen(), u.gen();
std::cout << u.size() << std::endl; // Prints "4".

Definition at line 1061 of file tgen.h.


The documentation for this struct was generated from the following files:
  • /home/runner/work/tgen/tgen/single_include/tgen.h
  • base.dox