C++

# Fill Array With Random Numbers C++

Arrays are used to hold items with the same data type in sequential memory areas. We can easily build arrays of various data types in C++. In this article, we will work on creating a random array containing random numbers in C++. So it will help you to create random numbers and store the associated number in an array. We will use the C++ library to create random numbers, which we will then use to populate our array.

## Methods For Generating Random Numbers In C++

The C++ programming language includes a built-in pseudo-random number generator as well as two methods for generating random numbers: rand() and srand(). Let’s go through the rand() and srand() methods in depth.

### Rand()

To get a random number, we use the rand() method. When invoked, the rand() function in C++ generates a pseudo-random number between 0 and RAND MAX. Whenever this method is used, it utilizes an algorithm that gives a succession of random numbers. We cannot consider the numbers created to be genuinely random because they are created by using an algorithm that employs a seed value; instead, we refer to such numbers as pseudo-random numbers.

### Srand()

The srand() method is frequently used in combination with the rand() method. If the srand() method is not used, the rand() seed is generated as if srand(1) had been used earlier in the program setup. Any other seed value causes the generator to begin at a new location.

Note that if you use rand() to produce random numbers without first executing srand(), your code will generate the sequence of the same integers every time it runs.

### Example 1

We are using the rand() method to generate random numbers in an array of an integer. First, we have declared a variable “MyNumber” with the data type integer. The variable “MyNumber” takes an integral value from the user. Then, we have an integer array “Rand” and on the next line, we have a for loop cycle which generates a random number over each iteration using the rand() method.

We take an array’s size and then define an array of that size. The rand() method generates random numbers, divides them by 10, and stores the remainder in an array at a specific position. The array will be printed after initialization.

#include <iostream>

using namespace std;

int main()

{

int MyNumber;

cout<<"Enter the array size number::";

cin>>MyNumber;

int Rand[MyNumber];

for(int r= 0; r <MyNumber; r++)

Rand[r]=rand()%10;

cout<<"\nArray Elements::"<<endl;

for(int r=0; r<MyNumber ; r++)

cout<<"Number of Elements "<<r+1<<"::"<<Rand[r]<<endl;

return 0;

}

The result of random numbers in an integer array is shown in the following image.

## Example 2

As discussed, srand() sets the seed for the rand() method. We built the method for filling an array with random values using the srand() method in c++. First of all, we have imported the c++ built-in library “time.h” which returns the current timestamp at the moment the function is called. As a result, we can assure that a distinct value is given to the srand() method as the parameter each time the program is performed.

Then, we have another built-in library, “stdlib.h” through which we can access both the rand and srand methods. We have the main function where the code is put into action. We have created an array as an “Array” of a custom size. The array’s size will be specified by the user. Next, we have used the srand method and passed the seed value “NULL” in it. Each time we run the program, a random and unique set of values is generated instead of the repeated values.

In the block of for loop, we have the rand() method which will produce a random number in each loop cycle. The cout command will print the random number of the given array size.

#include <iostream>

#include <time.h>

#include <stdlib.h>

using namespace std;

int main()

{

int snum;

cout<<"Enter number for array size: ";

cin>>snum;

int Array[snum];

srand(time(NULL));

for(int s=0; s<snum; s++)

{

int no = rand();

Array[s]=no;

}

for(int s=0; s<snum; s++)

cout<<Array[s]<<" ";

cout<<endl;

return 0;

}

The outcome of the random number of array size “4” is displayed on the terminal screen of Ubuntu.

### Example 3

We can also generate random numbers within the range. In the following program, we implemented the way to fill an array with random integers inside a range in C++. We have created an array as “Arr” which will take the size of the array when a user inputs it.

Then, we set seed time to “NULL” in the srand method. As the srand method is used, the program will create distinct sets of random numbers each time it is executed. After this, we have generated a random number within the range from 0 to 5. We use module 5 operators to call the rand method.

#include <iostream>

#include <time.h>

#include <stdlib.h>

using namespace std;

int main()

{

int randnum;

cout<<"Enter the array size: ";

cin>>randnum;

int Arr[randnum];

srand(time(NULL));

for(int i=0; i<randnum; i++)

{

int rnumber = rand()%5;

Arr[i]= rnumber;

}

for(int i=0; i<randnum; i++)

cout<<Arr[i]<<" ";

cout<<endl;

return 0;

}

As you can see, the random number generated lies in the range of 0 to 5 in the following piece of the image.

### Example 4

By default, the rand() function we saw before returns an integer result, which might create an overflow in some instances. As a result, we can utilize either a float or a double value in c++. We will generate the float random numbers in the following example code by converting the rand () function’s return value to ‘float’.

At first, I used a srand function that passed an unsigned data type specifier. Now only the non-negative value will be displayed, i.e., the positive value and zero, and the seed time set to NULL. Then, we have a for loop statement, which will loop the cycle to the 10 random values. The cout command casts the float data type by using the rand method.

#include <time.h>

#include <iostream>

using namespace std;

int main()

{

cout<<"The Random numbers lies between 0 and 1:"<<endl;

srand( (unsigned)time( NULL ) );

for (int n = 0; n < 10; n++)

{

cout << (float) rand()/RAND_MAX << endl;

}

return 0;

}

The program’s output is random numbers that lie in between the values 0 and 1, which are fractions. If we do not cast the return result of the rand() method to float, then 0 will be obtained as the random number.

## Conclusion

Therefore, we can create random numbers in C++ by utilizing the two methods, rand() and srand(). The method srand() provides the seed for creating random numbers, whereas the method rand() provides the sequence for the next random numbers. We have discussed the four illustration codes to generate random numbers in C++. We have also shown how to populate random integers in an array within a range.