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++

Follow the steps::

  • 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;
}

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 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 <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;
}

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

Also read: Find smallest prime divisor of a number in C++

Leave a Reply

Your email address will not be published. Required fields are marked *