Randomising a Vector in C++

In this article, we will attempt to create a vector having random values inside of it, where the length of the vector will be given through user input. In easy words, we will randomize a vector in C++.

A vector is the same as a dynamic array, which means that it is resizable based on the number of elements inside it. The size is accordingly changed when an element is inserted or removed/deleted. Vector elements are stored contiguously, i.e., their addresses of the memory blocks are consecutive. This makes the usage of iterators a possibility. The insertion and deletion of an element may have varying execution times since resizing may be taking place.

Vectors And Iterators

As stated previously, a vector is a dynamic and resizable array. The operations performed on it are done so through the use of pointers called iterators. They are used for pointing to the addresses of memory blocks of an array or sequence, which is stored in a contiguous form of storage.

There are certain functions associated with vectors and make their use easier. Some of them are:

  • begin ()
  • end()
  • rbegin()
  • rend()
  • cbegin()
  • cend()
  • crbegin()
  • crend()
  • push_back(*argument*)
  • pop_back()

We can show the practice of these iterators and functions as follows:

#include<iostream>
#include<vector> 

using namespace std;

int main()
{
    vector<int> ar;
    
    for(int i = 5; i <= 10; ++i)
        ar.push_back(i);
    
    cout<<"Output using begin() and end() functions : \n";
    for(auto ir = ar.begin(); ir != ar.end(); ++ir)
        cout<<*ir<<'\t';
    
    cout<<"\nOutput using cbegin() and cend() functions : \n";
    for(auto ir = ar.cbegin(); ir != ar.cend(); ++ir)
        cout<<*ir<<'\t';
    
    cout<<"\nOutput using rbegin() and rend() functions : \n";
    for(auto ir = ar.rbegin(); ir != ar.rend(); ++ir)
        cout<<*ir<<'\t';
        
    cout<<"\nOutput using rbegin() and rend() functions : \n";
    for(auto ir = ar.crbegin(); ir != ar.crend(); ++ir)
        cout<<*ir<<'\t';    
    
    return 0;
}

The output of the above code will be:

Output using begin() and end() functions : 
5    6    7    8     9    10
Output using cbegin() and cend() functions :
5    6    7    8     9    10
Output using rbegin() and rend() functions :
10   9    8    7     6    5
Output using crbegin() and crend() functions :
10   9    8    7     6    5

Randomising a Vector in C++

Now we shall create a function where the length of the required length of the random vector is taken as a parameter, and the vector array is returned to the calling function.

We will use the rand() and srand() functions from stdlib.h header file, and the time() function from time.h header file. The code will look like this (assuming that all the required header files are included in the program):

vector<int> Rand_Vector(int lngth)
{   
    srand(time(0));
    
    vector<int> rand_arr
    
    for(int i = 0; i < lngth; ++i)
        rand_arr.push_back(rand());
        
    return rand_arr;
}

This function takes the length of the required vector as input and returns the vector having random values for each of its elements. Suppose the ‘lngth’ argument was given as 6, then the returned array may look something like this :

9933   44921   6637  112  6001  57  55212

This function can easily be implemented into any program and can be easily understood and modified. Hence, in this article, we have gone through the concept of vectors and the process of creating a randomly generated vector of integers of a given length.

 

You may also like:

Generate a random password of a specific length in C++

Generate Random Alphanumeric String in C++

Leave a Reply