/*C++ program to generate random numbers.*/
#include<iostream.h>
#include<stdlib.h>
int main()
{
int i; //loop counter
int num; //store random number
randomize(); //call it once to generate random number
for(i=1;i<=10;i++)
{
num=rand()%100; //get random number
cout << num << "\t";
}
return 0;
}
If you do not use randomize(), numbers will be generated randomly but when you again run the program same output will come. the output after removing randomize ():
Note: always use randomize (), if you want to get random numbers at every run.
In this program, we used # just after about yourself information, because in the program we are using delimiter (#) to terminate string because about yourself section may contain more than one lines, so we have to need a special delimiter to terminate reading.
Generate random numbers example in C++
Output
If you do not use randomize(), numbers will be generated randomly but when you again run the program same output will come.
the output after removing randomize () :
Note: always use randomize (), if you want to get random numbers at every run.
In this program, we used # just after about yourself information, because in the program we are using delimiter (#) to terminate string because about yourself section may contain more than one lines, so we have to need a special delimiter to terminate reading.
need an explanation for this answer? contact us directly to get an explanation for this answer