It is a pseudo random generator which generates a random number between 0 and RAND_MAX. In order to generate random number in user defined range, this function can be used with modulus operator.
// Generates random number between 0 to 99
int r1 = rand() % 100;
// Generates random number between 1 to 100
int r2 = rand() % 100 + 1;
PROGRAM
/* Program to make user guess a number and comparing it with randomly generated number */
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int count=0,rno,num;
rno = rand()%10 + 1;
do
{
cout<<"\nENTER A NUMBER BETWEEN 1 TO 10: ";
cin>>num;
if(num==rno)
{
cout<<"\nBINGO!";
}
else if(num<rno)
{
cout<<"\nRAISE YOUR NUMBER!";
}
else
{
cout<<"\nLOW YOUR NUMBER!";
}
count++;
} while(num!=rno);
cout<<"\nYOUR SCORE IS "<<count;
return 0;
}
OUTPUT