Pages

Saturday, 23 April 2016

Generate Random Number in C++

Random number is generated by using srand(time(NULL)) and rand() function.For time we have to include time.h and for srand() and rand function we have to include stdlib.h.Here is the simple program which generate 10 random number.

#include<iostream>
#include<time.h>
#include<stdlib.h>
#define MAX_NUM 10
using namespace std;
void randnum(){
     int random;
     srand(time(NULL));
     for(int i=0;i<10;i++){
          random=rand()%MAX_NUM;
          cout<<random<<endl;
     }
}
int main(){
     cout<<"the ten random number is "<<endl;
     randnum();
     return 0;
}

No comments:

Post a Comment