• C Programming Video Tutorials

Random Number Generation in C



The stdlib.h library in C includes the rand() function that returns an integer randomly, between "0" and the constant RAND_MAX.

The rand() function generates pseudo-random numbers. They are not truly random. The function works on Linear Congruential Generator (LCG) algorithm.

int rand(void);

Example 1

The following code calls the rand() function thrice, each time it is likely to come with a different integer −

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

int main() {

   printf("%ld\n", rand());
   printf("%ld\n", rand());
   printf("%ld\n", rand());

   return 0;
}

Output

Run the code and check its output −

41
18467
6334

The rand() function doesn't have a built-in mechanism to set the seed. By default, it might use a system specific value (often based on the time the program starts).

Note: The srand() function is used to improve the randomness by providing a seed to the rand() function.

Example 2

The following program returns a random number between 0 to 100. The random integer returned by the rand() function is used as a numerator and its mod value with 100 is calculated to arrive at a random number that is less than 100.

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

int main(){

   for(int i = 1; i <= 5; i++) {
      printf("random number %d: %d \n", i, rand() % 100);
   }

   return 0;
}

Output

Run the code and check its output −

random number 1: 41
random number 2: 67
random number 3: 34
random number 4: 0
random number 5: 69

Example 3

You can also obtain a random number between a given range. You need to find the mod value of the result of rand() divided by the range span, add the result to the lower value of the range.

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

int main() {

   int i, num;
   int lower=50, upper=100;

   for (i = 1; i <= 5; i++) {
      num = (rand() % (upper - lower + 1)) + lower;
      printf("random number %d: %d \n", i,num);
   }
   
   return 0;
}

Output

Run the code and check its output −

random number 1: 91
random number 2: 55
random number 3: 60
random number 4: 81
random number 5: 94

The srand() Function

The stdlib.h library also includes the srand() function that is used to seed the rand() function’s random number generator.

You would use the following syntax to use the srand() function −

void srand (unsigned seed);

OR

int srand (unsigned int seed);

The seed parameter is an integer value to be used as seed by the pseudo-random number generator algorithm.

Note: If srand() is not initialized, then the seed value in rand() function is set as srand(1).

Usually, the srand() function is used with the value returned by time(NULL) (which represents the current time since the epoch) as a parameter to improve the randomness of the pseudo-random numbers generated by rand() in C.

Since the time value changes all the time, this will have different seed values, leading to more varied random sequences. As a result, if you generate random number multiple times, then it will likely result in different random value every time.

Example 1

Take a look at the following example −

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

int main() {

   srand(time(NULL));
   printf("Random number: %d \n", rand());
   
   return 0;
}

Output

Every time a new random integer between 0 to RAND_MAX will be displayed.

Random number: 1436786055 

Example 2

We can include srand() to generate a random number between a given range.

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

int main() {

   int i, num;
   time_t t;
   int lower = 100, upper = 200;
   srand((unsigned) time(&t));

   for (i = 1; i <=5; i++) {
      num = (rand() % (upper - lower + 1)) + lower;
      printf("random number number %d: %d \n", i, num);
   }
   
   return 0;
}

Output

Run the code and check its output −

random number number 1: 147
random number number 2: 171
random number number 3: 173
random number number 4: 112
random number number 5: 181

The random number generation offered by rand() function is not truly random. With the same seed, you'll always get the same sequence. It also has a limited range as it generates random numbers within a specific range (0 to RAND_MAX).

To improving the Randomness, you need to use a good seed value with high unpredictability like system time or a high-resolution timer. You can also use third party libraries for wider range random numbers.

Advertisements