How To Generate Random Numbers with rand() and srand() Functions in C and C++?


C and C++ programming languages provide rand() and srand() functions in order to create random numbers. Random numbers can be used for security, lottery, etc. In this tutorial, we will learn how to use a random number generating functions rand() and srand() with their attributes and specialties.

Random Numbers

Random numbers are sequential numbers that are different than each other always. Random numbers are important for security which can be used to generate UUID, Certificates, Passwords, etc. In order to generate random numbers we generally need a hardware-based source that generates random signals where these signals will be used to generate a random number. There are hardware components used to generate these random signals specifically.

PSEUDO Random Numbers

As using hardware-based random number generators are pricy or not always available we generally use random-like signal generators. If we are using this type of not completely random number generator we call these PSEUDO Random Number Generators. For example Network Interface Card, Sound Card or similar hardware can be used as PSEUDO Random Number Generator.

Seed Value

As creating randomness is a very hard job we can provide Seed for every random function execution to create randomness. Seed values are used to make a random start from the application point of view. Every time we need to use different seed values to make it perfect. We generally use functions like time, network traffic, etc where they are changing regularly.

RAND_MAX

While generating random numbers we need to set some start and end limits. The start limit is 0 but the end limit can be defined explicitly with the RAND_MAX macro like below.

#define RAND_MAX 100

rand() Function

rand() function is used to create random numbers without any parameters. This will create the same sequence in each run during the same execution.

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

int main(void) 
{

for(int i = 0; i<10; i++) 
   printf(" %d ", rand()); 

printf("\n");
return 0; 
}
rand() Function
rand() Function

We can see that every time we call rand() function we get the same random numbers.

LEARN MORE  Globally Unique Identifier (GUID) Generator For Linux, Windows, Java, PHP, C#, Javascript, Python

srand() Function

srand() function really generates pseudo-random integers.  We will also provide a seed value in an unsigned integer. We will make seed value the return value of the time(0) function which will be different every execution of the application.

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

int main(void) 
{ 
   srand(time(0));

   for(int i = 0; i<10; i++) 
      printf(" %d ", rand()); 

   printf("\n");
   
   return 0; 
}
srand() Function
srand() Function

We can see that every time we call the random function we get a different value which is completely random.

rand() Function vs srand() Function

rand() and srand() are very similar functions . rand() function actually calls the srand(1) which will generate always the same random number sequence.

Specify Different Random Number Range Explicitly

We have learned that we can use RAND_MAX  to limit generated random numbers. But start number will be . We can specifically define a start and max number by using mod operations of the C and C++ programming languages.

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

int main(void) 
{ 
   srand(time(0));

   for(int i = 0; i<10; i++) 
      printf(" %d ", (rand()%100)-20); 
   
   printf("\n");
   return 0; 
}
Specify Different Random Number Range Explicitly
Specify Different Random Number Range Explicitly

1 thought on “How To Generate Random Numbers with rand() and srand() Functions in C and C++?”

  1. Nice article. With the help of this article, I understood the concept of rand in the c programming language.

    Reply

Leave a Comment