C programming rand stdlib.h


In C programming the <stdlib.h> 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( );

Parameters:
void

Return type
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.In other words it does not provide a true random number so it has some disadvantages,what are those disadvantages will be discussed later.

Code example

printf(“%i”, rand( ) );

printf(“\n%i”, rand() );

Output in VS,

41
18467

Code::blocks,will also give the same random numbers.This is because rand function will always generate the same values.





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].

Code example

for(int i=0 ; i<10 ; i++)
{
printf(“%i”   , 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.

Code example

unsigned int count=0 ,
val ;

while( true )
{
if( (val=rand() % 201 ) >= 50 )
{
++count ;
printf(“%i   “, 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.The drawbacks are discussed below.


 



Why ‘%’ should not be used???

At first glance the using ‘%’ operator on the rand() generated value might seem a probable solution.But it isn’t so,there are two drawbacks in using the ‘%’ operator.They are given below.

i) Many pseudo-random-number generator(including the rand() function) give remainders that aren’t very random,when the integers used for the division are small.For example,it is not uncommon for successive results of rand() to be alternately even and odd.If the number used is 2,successive results of rand() % n (n is the divisor) will alternate between 0 and 1.

Code example

for(int i=0 ; i<4 ; i++)
{
printf(“%i   ” , (rand()%2) );
}

Output

1 0 0 1

ii)If the value used for division (say ‘n’) is large, and the generated maximum value is not evenly divisible by ‘n’,some remainders will appear more often than others.For example,if the maximum is 32767 and ‘n’ is 2000,17 generated values (500,2500,…,30500,32500) would map to 500,while only 16 generated values (1500,3500,…,31500) would map to 1500.And this gets worse if ‘n’ becomes larger.

The above two reasons is enough to justify why rand() or any pseudo-random number generator should not be used.