# Generate a Matrix of Random Numbers in C++

In this tutorial, we will learn how to generate matrix of random numbers in C++.

A matrix is known as a two-dimensional array/vector.  It is represented in the form of rows and columns. It is used in many practical situations, and for many general purposes. It is synonymously associated with the form of a table, containing horizontal rows and vertical columns.

Usually, a matrix is expected to carry some meaningful data. It holds some useful data relevant to some real word entity or quantities. However, in many cases, it is required to create this data when it cannot be procured from any single source. This often occurs when machine learning models are needed to be trained with large sets of data. Hence, the need arises to create random two-dimensional arrays.

The full program is at the bottom. Scroll down if you want to skip down the procedure.

## Using the Random functions

In C++, we have the declaration of the array given below:

```int ar;
for(int i = 0; i < 3; ++i)
{ for(int j = 0; i < 3; ++j)
{ cin >> a[i][j];}
}```

This helps initialize an array of 3 x 3 size and take user-input for each element row-wise.

To create the array, having each element assigned and decided randomly, we use the header file stdlib.h to our program. This contains the rand() and srand() functions. The srand() function is used for setting the seed for generating random values through the rand() function. The full form of srand is ‘Set seed for rand.’ A number is generated between 0 and RAND_MAX.

If the rand() function is used without calling the srand() function first, then the same random integers will be returned every time the program is run since it will be treated as srand(1), or in other words, the seed for the random number generator will be fixed.

```#include<iostream>
#include<stdlib.h>

void main()
{
for(int i = 0; i < 3; ++i)
cout>>rand()>>'\t';
}```

The output, when the program is run for the first time, would be:

```593   12   9962

```

The second time, the output would still be:

`593   12   9962`

No matter how many times the program, the output will always be:

`593   12   9962`

However, if we initialize the srand() function using the time.h header file with the current system, which will be different for every single time the program is run. This would result in the rand() function delivering random values every time the function is called, due to the existence of a different seed value for every execution. Hence, when we change the code to this:

```#include<iostream>
#include<stdlib.h>
#include<time.h>

void main()
{
srand(time(0));

for(int i = 0; i < 3; ++i)
cout>>rand()>>'\t';
}```

We may get the first output as:

`593   12   9962`

The second output will be different, like:

`123   9331 1414`

And the third output would also be distinct:

`444     23124   9`

## Create a Random Matrix in C++

Combining the method of matrix traversal and the random number generating functions, we can create a C++ code that would implement the original concept of creating a two-dimensional array full of elements generated randomly. The code is as follows(for a 3 x 3 matrix) :

```#include<iostream>
#include<stdlib.h>
#include<time.h>

using namespace std;

int main()
{
srand(time(0));

int a, i ,j;

for( i = 0; i < 3; ++i)
{for( j = 0;  j < 3; ++j)
{a[i][j] = rand();}
}
for( i = 0; i < 3; ++i)
{for( j = 0; j < 3; ++j)
std::cout<<a[i][j]<<'\t';
std::cout<<'\n';
}
return 0;
}```

The dimensions of the matrix can be changed according to the user’s requirements. However, the end result would be the generation of a matrix filled with random integer elements, which is exactly what was required. The output would be:

```593   12   9962
123  9331  1414
444  23124   9```

In this article, the concepts of matrices and random functions were discussed, as well as methods of how they can be used. These two concepts were also combined to create a two-dimensional array having each element of it generated randomly.

You may also like:

C++ program to print the boundary elements of a matrix

How to print Identity matrix of given size in C++