# Generate a random array in C or C++

Today, in this tutorial, we will get to know how to generate a random array with random values in C and C++. So you will learn how to generate a random number and store the corresponding number in an array. Below you can see source codes of generating random numbers in C and C++.

## Method to Generate random array in C or C++

• Get the size of an array and declare it
• Generate random number by inbuilt function rand()
• Store randomly generated value in an array
• Print the array

## Rand() function::

Random value can be generated with the help of rand() function. This function does not take any parameters and use of this function is same in C and C++. Syntax to get random one digit number::

```int i=rand()%10;
cout<<"Random number::"<<i<<endl;

Output::
Random number::6```

## C++ program to generate a random array

Now, we will see a C++ program to generate a random array. Here we generate values between 0 and 99 by using inbuilt function rand() and assign it to a particular position in an array. Here we take the size of an array and then declares an array of particular size. Random numbers are generated by using rand() function and that number is divided by 100 and the remainder is stored in an array at a particular location. After initializing, the array is printed.

```#include <iostream>
using namespace std;

int main()
{
int sz;
cout<<"Enter the size of array::";
cin>>sz;
int randArray[sz];
for(int i=0;i<sz;i++)
randArray[i]=rand()%100;  //Generate number between 0 to 99

cout<<"\nElements of the array::"<<endl;

for(int i=0;i<sz;i++)
cout<<"Elements no "<<i+1<<"::"<<randArray[i]<<endl;
return 0;
}
```

Run this code online
Output::

```Enter the size of array::6

Elements of the array::
Elements no 1::83
Elements no 2::86
Elements no 3::77
Elements no 4::15
Elements no 5::93
Elements no 6::35```

## C program to generate a random array

Now, we will see a C program to generate a random array. Here we generate values between 0 and 99 by using the inbuilt function rand() and assign it to a particular position in an array. Here we take the size of an array and then declares an array of a particular size. Random numbers are generated by using rand() function and that number is divided by 100 and the remainder is stored in an array at a particular location. After initializing, the array is printed.

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

int main(void)
{
int sz;
printf("Enter the size of array::");
scanf("%d",&sz);
int randArray[sz],i;
for(i=0;i<sz;i++)
randArray[i]=rand()%100;   //Generate number between 0 to 99

printf("\nElements of the array::");
for(i=0;i<sz;i++)
{
printf("\nElement number %d::%d",i+1,randArray[i]);
}
return 0;
}
```

Run the above program online
Output::

```Enter the size of array::5
5
Elements of the array::
Element number 1::56
Element number 2::23
Element number 3::71
Element number 4::90
Element number 5::75```

### 7 responses to “Generate a random array in C or C++”

1. James says:

The C++ example doesn’t work for me. It says sz has to be a constant value. How did you get it to work?

• hSaruque Ahamed Mollick says:

I have just checked the code and everything is fine and working well. You can use online C++ compilers if you wish

• Pavel says:

You need to declare it as const int sz. For all future folks who dont know.

2. Liril Silvi says:

How to write float value of random number between a range in an array lets say, “300.15 to 330.55”

3. Fatima says:

How to find the largest number from using the same method?

4. Porter Goldman says:

It generates the same numbers every single time for me is that not the case for everyone?

• Shubham Loya says:

Rand function generates random number. Please check on online compilers