The C++ <cstdlib> rand function compute a sequence of pseudo-random integers in the range 0 to RAND_MAX.This function is equivalent to the C stdlib.h rand( ) function.The declaration of the function is shown below.
|int rand( );|
int -A pseudo-random integers.
Some points to note:
i) The value of RAND_MAX or the largest value this function can generate should be at least 32767.
ii) The implementation shall behave as if no library function calls the rand function.
iii) This function does not provide a random value,it provides pseudo-random numbers.You should prefer the C++ random number generator,the <random> header file.
<< rand( ) << endl ;
/*Checking the RAND_MAX value */
cout<< RAND_MAX << endl ;
Output in Code::Blocks,
Getting random number within specific range
If you look at the output of the above program you will notice that the values are disorderly distributed meaning the first value generated(41) is small but the second value generated(18467) is very large.Such disproportionate generation of random numbers can sometimes be useless to us.
Suppose if your program requires a random numbers within certain range then the generated random numbers cannot be used as the random number may not fit into the desired range.So,to get numbers within the specific range our only option is to process the generated numbers using some methods and make them fit within the required range.One traditional way of doing this is to apply the modulus(%) operator to the generated number.
The operator % gives the remainder in performing division by the right hand operand on the left hand operand.The right hand value must be the value just next to the highest value of the range within which the random value is required.Suppose if you want a random number within the range [0 , 200],the right hand value must be 201.Since diving any number by 201 will always give the remainder lesser than 201 and the remainder will fit exactly into the range.Consider the code below which tries to generate 10 random numbers within [0, 200].
cout<< (rand() % 201) << ” ” ;
Output in Code::blocks,
41 176 103 169 74 46 21 12 28 143
The output is all within the desired range [0 , 200].Now you can be rest assured that your remaining program will work just perfectly fine.
If we think deeper however we can’t relax yet,the code above works only if the lowest value of the range is 0.So what happens say if the range within which the random number required is [50 , 200].In this case there can be two solutions,either we neglect all the values lesser than 50 or any values lesser than 50 should be added with some other random number to increase it’s value to 50 or more.
The first solution can be easily achieved by using ‘while’ statement and looping it until we have attained 10 random numbers within the range.And when we have reach the limit we can break out of the loop.A code example is given below.
while( true )
if( (val=rand() % 201) >= 50 )
cout<< val << ” ” ;
if( count==10 ) break ;
Output in Code::blocks,
176 103 169 74 143 77 166 144 112 89
The program is simple:the ‘count’ keeps track of the number we have obtained within the range and when it reaches 10 we terminate the loop by calling ‘break’.
For the second solution I leave it to the reader to come up with the suitable code.And mind you if you need any help comment below.
Although using the % operator solves the problem of rand() generating out of range values,it should be duly noted that it still have some drawbacks.I won’t be discussing here the drawbacks so please kindly visit the link Why ‘rand’ should not be used or why random number generator with distribution should be preferred to read about them.