HOW TO GENERATE RANDOM NUMBER IN C++


C++ CODE FOR HOW TO GENERATE A RANDOM NUMBER
This code is an example of how to generate a random number in C++  language using srand function which changes the number randomization time respect to time.Time is most random thing which is changing at every moment.

HAPPY CODING!



//random generation of numbers

#include<iostream>
#include<string>
#include<cstdlib>//library for random number genertion
#include<ctime>//library for srand function


using namespace std;


int main()
{
    srand(time(0));/*this will allow rand() to generate according to changing time*/
    int num;
    num=1+rand()%50;/*it will generate random number from 1 to 50*/
    cout<<"RANDOM NUMBER GENERATED IS:"<<num<<endl;

    system("pause");
    return 0;

}




No comments:

Post a Comment