How to Generate Random Numbers in C++ Program

Random numbers are mostly used in games development. The srand () function generate a number between zero to maximum . In this problem we generate 5 random numbers.

Source Code 

#include<iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main ()
  {
  int a,b,c,d;
  srand ((unsigned)time (NULL));
  a=rand();
  cout<<" Generate First Random Number  : " <<a <<endl; 
  b=rand();
  cout<<" Generate Second Random Number : " <<b<<endl;
  c=rand();
  cout<<" Generate First Random Number  : " <<c<<endl;
  d=rand();
  cout<<" Generate First Random Number  : " <<d<<endl;
  
  }

Output of the Program

C++ Programming

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment