Table of Contents

IN this post, we will see how to fill array with random number in C++.

Arrays are the most commonly used data structures in the programming domain. We use arrays to store values of the same data type together at contiguous memory locations. In C++, we can create arrays of different data types in a very simple way. In this article, we will create an integer array and we will understand how we can populate our integer array with random numbers. An integer array is defined to store integral values. When we define an integer array, it is by default empty and does not contain any values initially. We will generate random numbers using the C++ library and then we will fill our array with these random numbers.

## The rand() Function in C++

The `rand()`

function in C++ returns a pseudo-random number in the range [0, RAND_MAX) each time it is called. It uses an algorithm that returns a series of random and unrelated numbers each time the function is called. Since the numbers are generated using an algorithm that uses a seed value, we do not consider the numbers generated to be actually truly random, rather, we call such numbers pseudo-random numbers.

We often use the `srand()`

function in conjunction with the rand() function. This function is used to provide a seed value for the random generation of numbers. If we want to generate a different set of numbers each time we execute the rand() function, it is absolutely critical to provide a random seed each time. **In case we do not provide a different random seed, we will get the same sequence of numbers each time we execute the rand() function. **

Although we can provide a random different seed value on our own to the srand() function, we often make use of the time() function. The time() function returns the current timestamp at the time of execution. Therefore, we can ensure that each time your program is executed, a different value will be passed to the srand() function as the parameter. It, in turn, ensures that a different set of values are produced by the random function, and we do not get the repeating and recurring values each time.

The C++ library, *stdlib.h*, contains both of the rand() and srand() functions. On the other hand, RAND_MAX is a constant defined in the *ctsdlib.h *library. The time() function is defined in the time.h* *library.

The rand() function outputs the number in the range [0, RAND_MAX]. It means a large number can be returned by the function. Therefore, to limit the range of the output, we often perform the modulo operation on the result. For example, if we want to limit our output to a number N, we will perform the modulo of output with N. However, in this process, we may get duplicate results, in essence, the numbers can be repeated.

## How to Fill the Array With Random Numbers in C++?

As we have already discussed, we can produce a series of random numbers with the help of the rand() function in C++. We will use the rand() function to generate a series of random numbers and fill them in the array with the following steps.

- First, we will create an integer array of a custom size. As the arrays are not initialized by default in C++, the array will contain garbage values initially.
- Then, we will use the srand() function with a seed value as time(NULL). As we have already discussed, the time() function returns the current timestamp which will be different each time we execute the program. It will ensure that every time we execute the program, a random and different set of values is produced instead of the recurring values.
- After seeding the srand() with the current timestamp, we will loop through the array using a loop control statement.
- In each loop cycle, we will generate a pseudo-random number using the rand() function.
- We can insert the pseudo-random number into our array using the array index and move forward to the next position in the array. This is because the maximum number returned by the rand() function i.e. RAND_MAX is equal to the maximum value of an integer in C++, in essence, INT_MAX. Therefore, no overflow or underflow shall occur.

**We have implemented the above approach to fill the array with random numbers in C++ as follows.**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <iostream> #include <time.h> #include <stdlib.h> using namespace std; int main() { int n; cout<<"Enter size of the array: "; cin>>n; int arr[n]; srand(time(NULL)); for(int i=0;i<n;i++) { int num = rand(); arr[i]=num; } for(int i=0;i<n;i++) cout<<arr[i]<<" "; cout<<endl; return 0; } |

Output:

1 2 3 4 5 6 7 8 9 |
stark@stark:~/eclipse-workspace/Aditya$ g++ fillArray.cc stark@stark:~/eclipse-workspace/Aditya$ ./a.out Enter size of the array: 5 762014512 1882285997 62966291 1156950587 1270254940 stark@stark:~/eclipse-workspace/Aditya$ ./a.out Enter size of the array: 5 176497408 1283106501 549587926 906171921 1077257850 |

You can notice the difference in output which shows that a different set of random values is produced each time the code is executed.

## Further reading:

## How to Fill the Array With Random Numbers in a Range in C++?

If you don’t want such large numbers in the output array, you can perform the modulo operation on the result produced by the rand() function say M. Suppose that we have to use the random numbers between 0 and N. In such a case, we will perform the modulo operation M%N to create random number in the range 0 to N-1 in the array.

In the following program, we have implemented the approach to fill an array with random numbers within a range in C++.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <iostream> #include <time.h> #include <stdlib.h> using namespace std; int main() { int n; cout<<"Enter size of the array: "; cin>>n; int arr[n]; srand(time(NULL)); for(int i=0;i<n;i++) { int num = rand()%n; arr[i]=num; } for(int i=0;i<n;i++) cout<<arr[i]<<" "; cout<<endl; return 0; } |

Output:

1 2 3 4 5 6 7 8 9 10 11 12 13 |
stark@stark:~/eclipse-workspace/Aditya$ g++ fillArray.cc stark@stark:~/eclipse-workspace/Aditya$ ./a.out Enter size of the array: 5 3 2 1 3 0 stark@stark:~/eclipse-workspace/Aditya$ ./a.out Enter size of the array: 5 1 2 3 0 3 stark@stark:~/eclipse-workspace/Aditya$ ./a.out Enter size of the array: 5 4 1 1 1 4 |

Notice that the output values are now in the range [0,5), in essence, only 0, 1, 2, 3, or 4 are produced as output. Also, observe that certain values are repeated as we have already mentioned that it is a side effect of trimming that certain values may be repeated.

## Conclusion

We often work with random values and arrays and this article gives a detailed insight on how we can fill array with random numbers in C++. We have seen that we can produce the random values using the `rand()`

function and use the `srand()`

function to provide a seed. We have also seen how to fill an array with random numbers in a range in C++.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning!