In this article, you will learn about random number generator in C programming using rand( ) and srand( ) functions with proper examples.

random number generator in c

Random numbers are used in various programs and application especially in game playing.

For this, we have standard library function rand( ) and srand( ) in C which makes our task easier and lot more fun.

C standard library function rand is defined in the stdlib.h header file.

#include<stdlib.h>

C library function rand( )


Function declaration of rand( )

int rand( void )

This function returns an integer value ranges between 0 and RAND_MAX.

RAND_MAX is a symbolic constant defined in the header file stdlib.h which value is in the range of 0 to at least 32767. This is the maximum value for 16-bit integer or 2 bytes.

Therefore, it is clear that every number between 0 and 32767 has an equal probability of occurrence.

C library function srand( )


Function declaration of srand( )

void srand( unsigned int seed )

where,

seed = integer value used as seed by the pseudorandom number generated by rand.

This function seeds the random number generated by the function rand( ).

srand( ) does not return any value.

Let’s take a deeper look in the following example:

C program to generate random number for rolling a six sided die using rand

//use of rand function

#include<stdio.h>
#include<stdlib.h>

int main()
{
   int counter; //counter variable

   //displaying random numbers between 1 and 6
   for( counter = 1; counter <= 10; ++counter )
   {
      printf("%5d", 1 + ( rand() % 6 ));

      //for displaying value in new line
      if (counter % 5 == 0)
      puts("");
   }
   return 0;
}

[adsense1]
Output

c library function rand

Explanation

In the above program, we have rolled a die for 10 times which is determined by the counter variable.

We know that die have only 6 sides but rand function generates random numbers up to 32767. Therefore we have used scaling factor to achieve our goal.

rand() % 6

Again, the above line of code generates integers in the range of 0 to 5. For displaying numbers from 1 to 6, we have simply added 1 to each output.

Though we have generated random numbers in the range of 1 to 6 executing the program again produces the same result.

Now you might be wondering “How can these be random numbers?”

Let me explain:

Function rand generates pseudorandom numbers. These numbers are random but the sequence is repeated every time we run the program which is the important characteristic of function rand.

For randomizing this sequence, C provides standard library function srand( ) which we have discussed earlier.

srand takes an unsigned integer seed to randomize the sequence of numbers.

Common Programming Error
Don’t get confused between rand and srand. Using rand in place of srand will result in error.

 

Let us generate random numbers using srand.

C program to generate random numbers using srand( )

//use of srand function

#include<stdio.h>
#include<stdlib.h>

int main()
{
   int counter; //counter variable
   unsigned int seed; //for randomizing numbers

   printf("Enter the value of seed: ");
   scanf("%u", &seed);

   //randomizing numbers using seed
   srand( seed );

   //displaying random numbers between 1 and 6
   for( counter = 1; counter <= 10; ++counter )
   {
      printf("%5d", 1 + ( rand() % 6 ));

      //for displaying value in new line
      if (counter % 5 == 0)
         puts("");
   }
   return 0;
}

Output

#1
c library function srand

#2
srand function output

#3
srand function in c

Explanation

In this program, we have used seed to randomize every sequence.

This is all about random number generation in C programming.