|
tgen
|
tgen is a C++ library to help you generate random stuff, useful for testcase generation (such as jngen or testlib). The code is in a single file tgen.h, that should be added to your directory.
The first thing is to register the generator. That defines the seed for random generation and parses the opts.
There are:
and operations for specific data types:
All data types specified above define a generator, that when called upon will generate a uniformly random instance with the given constraints. Let's see an example with tgen::sequence:
This will create a sequence generator representing the set of all sequences with 10 values from 1 to 100.
Every generator of type Gen has a method gen(), that returns a Gen::instance representing an element chosen uniformly at random from the set of all valid elements from the current state of the generator. A Gen::instance can be fed to std::cout to be printed.
In our example, we can call gen() to generate and print a random sequence of 10 elements from 1 to 100.
The nice thing is that we can add restrictions (specific to each type) to the generator, shrinking the set of valid arrays. For example, we can add the restriction that the first and second elements of the sequence have to be the same.
The returned instance can also be modified by some deterministic operations (specific to each type).
Finally, there can be random operations defined for the type instance.
Combining everything into one line:
Calling this code with ./a.out -n 100 will generate a random number from 1 to 100.
Random 20 distinct values from 1 to 100.
Random Palindrome of length 7.
Random 3 runs of 4 equal numbers. Values between runs are distinct.
Random DNA sequence of length 8 with no equal adjacent values.
Random binary sequence of length 10 with 5 1's that start with 1.
Random 1-based permutation of size 5 with only one cycle.
Inverse of a random odd permutation of size 5.