C++ program example--random number


Generate random number

This C++ example program is to generate random numbers in two ranges. The first range is from 1 to 6 and the second is from 50 to 100.

#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {

srand(time(0));//initialize random number generator for the first range
int x=rand()%6+1;//generate random numbers from 1 to 6
srand(time(0));//initialize random number generator for the second range
int y=rand()%51+50;//generate random numbers from 50 to 100
cout<<"Random number in the first range:"< cout<<"Random number in the second range:"<

system("PAUSE");
return EXIT_SUCCESS; }




Comments

victor comment

 victor

It worked for me in MS Visual Studio 2010 only after I added:

#include <time.h>


2013-08-13



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.