Tuesday 12 August 2014

How to Generate Random Numbers in C++ Program

Structure of the Problem Requirements 

In this problem we will learn how to generate random numbers in C++ Programs. Random numbers are mostly used in games development. These Function exist in <ctime> and <cstdlib> header files which exist in C++ library . The srand () function generate a number between zero to maximum . In this problem we generate 5 random numbers. Here is the source code of this problem which help you in better understanding.

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
Generate Random Number

Share it Please
asad

About Author!

Asad Niazi is Software Engineer , Programmer, Web Developers and a young mentor of Tech Solutions Desk and Blogging Solutions . Asad Love to writes about Technology, Programming, Blogging and make money online.

0 comments:

Post a Comment