In C++ we can use some of miscellaneous
and useful functions of stdlib.h to
generate random numbers which are rand( ),
srand( ),random( )and randomize( ).
Simply
to generate a random number, you can use rand( ) functions that produces a random
number in the range 0 to RAND_MAX. The RAND_MAX is also defined in stdlib.h which gives you the maximum
value returned by rand( ). For example if you execute this code given below it
will generate 5 random numbers.
#include
<iostream.h>
#include
<stdlib.h>
int
main()
{
int
num,i;
for(i=0;i<5;++i)
{
num=rand();
cout<<num<<endl;
}
}
Output:
Now if you run this same program
you will notice same random numbers this program is generating.
2nd Run –
3rd Run –
4th Run –
So, in order to generate a random
number when ever you run the program you need to use srand( ) function in the above program. The srand( ) function
starts generating a number from a point that depends on the value we pass to
srand( ) as argument. Therefore, to generate a different number every time this
function is executed we need seed (start) the function with a truly variable
value such as system time. Now we can change the above code as follows:
#include
<iostream.h>
#include
<stdlib.h>
#include
<conio.h>
#
include <time.h> //to get system time through time_t
int
main()
{
int
num,i;
unsigned
int sval;
time_t
t;//t is a time type variable
sval=(unsigned)time(&t);
srand(sval);
for(i=0;i<5;++i)
{
num=rand();
cout<<num<<' ';
}
getch();
}
Output:
1st Run -
2nd Run –
3rd Run –
Note** Remember one thing: srand( ) function should be used just once before
the loop starts.
Generating a random number with a specified range:
The rand( ) function by default generates a number in range
0 to RAND_MAX. Now if you want to generate a number with our own specified
range say – 5 to 14, then you can change the rand( ) as below:
(rand( ) % U) + L
(rand( ) % 10) + 5
Here your L = 5 and U = 10
random( ) and rnadomize( ):
in order to generate in users’ specified range we can use
two more macros, also defined in stdlib.h they are random( ) and randomize( ).
These two also having similar function as that of rand( ) and srand( ).
To generate a random number between 0 – 9 you can write:
random(10);
Output can be any one of these:
0 1 2 3 4 5 6 7 8 9
Because random( ) function generates a random number maximum
up to n – 1.
So, to generate a random number between L – U (where L is
your lower limit and U is your upper limit)
You should write:
random(U – L + 1) + L
Example: random(100 – 50 + 1) + 50 (It will generate a
number within the range 50 – 100)
Note** randomize ( ) function internally
implements a macro that calls time function of time.h. So, you need to use
time.h header file in your program to implement random ( ) function.
Example 1:
//Program
to generate 5 random numbers in the range 5 - 14
//program
is using srand( ) and rand( ) functions.
#include
<iostream.h>
#include
<stdlib.h>
#include
<conio.h>
#
include <time.h>
int
main()
{
int
num,i;
unsigned
int sval;
time_t
t;
sval=(unsigned)time(&t);
srand(sval);
for(i=0;i<5;++i)
{
num=(rand()%10) + 5;
cout<<num<<' ';
}
getch();
}Output:
1st
Run
2nd
Run
3rd
Run
4th
Run
Example
2:
//Program
to generate 5 random numbers in the range 5 - 14
//program
is using randomize( ) and random( ) functions.
#include
<iostream.h>
#include
<stdlib.h>
#include
<conio.h>
#
include <time.h>
int
main()
{
int
num,i;
randomize();
for(i=0;i<5;++i)
{
num=random(14-5+1)+5;
cout<<num<<' ';
}
getch();
}
Output:
1st Run
2nd Run
3rd Run
4th Run
Note** For official explanation of any inbuilt library function or keyword you can always refer to the HELP menu of TurboC++ 3.0 IDE. The main reason behind writing all these contents only is to explain the topics in best possible way using examples and other ways of illustration. That's why wherever it is possible I have added snapshots of screen outputs or snaps of program code with graphical illustrations. Please refer to all those resources.
AWESOME helped alot!!!!
ReplyDeletethank you very much...this was very helpful
ReplyDeletethank you very much ..this was helpful
ReplyDeletethank you very much ..this was helpful
ReplyDeleteHelpful
ReplyDeletethanks..was really helpful
ReplyDeletecool c++ ...cool programs.....cool online teacher .....
ReplyDeletethanks a lot........
Unable to use randomize(), error undeclared. time.h included
ReplyDeleteUnable to use randomize(), error undeclared. time.h included
ReplyDeleteReally helpful.. thanks
ReplyDeletewell explain
ReplyDeletewell explained
ReplyDeletenice explanation gr8 XD
ReplyDeleteThanks for sharing information to us.................................
ReplyDeleteReally helpful
ReplyDeleteReally helpful to my child
ReplyDeleteperfect defination
ReplyDeleteThanks bro
ReplyDeleteorganized explanation
ReplyDeleteThanks for explanation it really helps
ReplyDeleteThank you. Nicely explained.
ReplyDelete