Generating Test Cases (generate() and generate_n() in C++) Last Updated : 29 May, 2017 Comments Improve Suggest changes 4 Likes Like Report Generating test cases for array programs can be a cumbersome process. But the generate and generate_n functions in the STL (Standard Template Library), come handy to populate the array with random values. generate() The generate functions assigns random values provided by calling the generator function 'gen' to the elements in the range [begin, end). Notice that begin is included in the range but end is NOT included. Following code demonstrates the implementation of generate : CPP // C++ program to demonstrate generate function in STL #include <bits/stdc++.h> using namespace std; // function to generate random numbers in range [0-999] : int randomize() { return (rand() % 1000); } int main () { // for different values each time we run the code srand(time(NULL)); vector<int> vect(10); // declaring the vector // Fill all elements using randomize() generate(vect.begin(), vect.end(), randomize); // displaying the content of vector for (int i=0; i<vect.size(); i++) cout << vect[i] << " " ; return 0; } Output : 832 60 417 710 487 260 920 803 576 58 NOTE : The output would be different each time we run the code because of srand. If we remove srand, we would get the same set of random numbers every time we run the code. generate_n() The generate_n does the same job as generate upto n elements starting from the element pointed to by the begin iterator. The following code demonstrates the working of generate_n : CPP // C++ program to demonstrate generate_n() function in STL #include <bits/stdc++.h> using namespace std; // function to generate random numbers in range [0-999] : int randomize() { return (rand() % 1000); } int main () { // for different values each time we run the code srand(time(NULL)); vector<int> vect(10); // declaring the vector // Fill 6 elements from beginning using randomize() generate_n(vect.begin(), 6, randomize); // displaying the content of vector for (int i=0; i<vect.size(); i++) cout << vect[i] << " " ; return 0; } Output : 177 567 15 922 527 4 0 0 0 0 NOTE : Here also, the output would be different each time we run the code because of srand. If we remove srand, we would get the same set of random numbers every time we run the code. Comment K kartik Follow 4 Improve K kartik Follow 4 Improve Article Tags : C++ STL CPP-Library Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like