/*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
First run: 88 90 76 3 67 43 20 31 28 1 Second run: 19 89 87 6 99 25 66 29 96 69If 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 () :
First run: 66 8 90 99 7 45 15 51 44 80 Second run: 66 8 90 99 7 45 15 51 44 80Note: 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