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 :
// C++ program to demonstrate generate function in STL#include <bits/stdc++.h>usingnamespacestd;// function to generate random numbers in range [0-999] :intrandomize(){return(rand() % 1000);}intmain (){// for different values each time we run the codesrand(time(NULL));vector<int> vect(10);// declaring the vector// Fill all elements using randomize()generate(vect.begin(), vect.end(), randomize);// displaying the content of vectorfor(inti=0; i<vect.size(); i++)cout << vect[i] <<" ";return0;}chevron_rightfilter_noneOutput :
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 :// C++ program to demonstrate generate_n() function in STL#include <bits/stdc++.h>usingnamespacestd;// function to generate random numbers in range [0-999] :intrandomize(){return(rand() % 1000);}intmain (){// for different values each time we run the codesrand(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 vectorfor(inti=0; i<vect.size(); i++)cout << vect[i] <<" ";return0;}chevron_rightfilter_noneOutput :
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.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Recommended Posts:
- Test Case Generation | Set 5 (Generating random Sorted Arrays and Palindromes)
- Subset array sum by generating all the subsets
- Generating RGBA portable graphic images through C++
- std::generate in C++
- Code to generate the map of India (with explanation)
- C++ program to generate random number
- Generate elements of the array following given conditions
- Generate all the binary strings of N bits
- Generate a string with maximum possible alphabets with odd frequencies
- Minimum number of coins that can generate all the values in the given range
- Minimum flips required to generate continuous substrings of 0’s and 1’s
- Generate an array of given size with equal count and sum of odd and even numbers
- Generate Array whose sum of all K-size subarrays divided by N leaves remainder X
- Generate a random permutation of elements from range [L, R] (Divide and Conquer)
- Generate permutation of 1 to N such that absolute difference of consecutive numbers give K distinct integers
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Generate an array B[] from the given array A[] which satisfies the given conditions
- AKS Primality Test
- bitset test() in C++ STL
- Vantieghems Theorem for Primality Test


