Skip to content

VectorRandomizer

afaji edited this page Mar 20, 2017 · 1 revision

VectorRandomizer

VectorRandomizer randomly generates a vector.

To begin, you have to include

#include "tcrand/vector.hpp"

This will enable VectorRandomizer class. Next, you can create your randomizer object as follow:

tcrand:: VectorRandomizer<T> vector_rand;

In this case T is the list type that you want to randomize, for example int or string

Setting the Engine

Setting the randomizer engine is required for VectorRandomizer. This is done by calling method engine(F) where F is a functor that returns a random value of T. This can be defined with a function, struct, or a lambda, depends on your preferences. For example, let's generate a list of integers where each element is from 0 to 100. We can do this by:

VectorRandomizer<int> vector_rand;
vector_rand.engine([]{return rand_int(0, 100);} );

rand_int is a built-in function from tcrand that you can call to randomly generates an integer. For more info on this, see Utility Functions.

Alternatively, if you don't prefer a lambda, you can do:

int f(){
    return rand_int(0, 100);
}

...

VectorRandomizer<int> vector_rand;
vector_rand.engine(f);

Setting Parameters

The parameters you may set in VectorRandomizer are:

  • length(N) your generated list will have a length of N
  • unique() your generated list's elements are unique
  • distinct_elements(D) your generated list will have exactly D distinct elements. This will conflict with unique

Examples

In this example, we want to randomly generate a vector of string, where each string is lowercase and has a length of 3 to 5. Additionally, we want the list to have exactly 5 distinct elements.

	//define the object
	StringRandomizer str_rnd;
	vectorRandomizer<string> vector_rnd;

	//define the string
	str_rnd.charset("[a-z]").length(3, 5);
	
	//define the list
	vector_rnd.length(20).distinct_elements(5);
	
	//embed the string randomizer into the listrandomizer
	vector_rnd.engine( [&] {return str_rnd.next();} );

	//obtain and print the result
	vector<string> res = vector_rnd.next();
	for (string v:res)
		cout<<v<<endl;
Clone this wiki locally