rand() and srand() in C / C++
In this tutorial, we'll explore the syntax, usage, and examples of the C++ STL functions rand() and srand().
What exactly is rand()?
The C++ STL's built-in rand() function is defined in the header file cstdlib>. Random numbers are produced using the rand() function. This function is used in our programming to produce random numbers.
For instance, if we are creating a snakes and ladders game in C++ and need to produce any random number between 1 and 6, we can use the rand() function.
Every time this function is used, an algorithm that produces a succession of unrelated numbers is used to generate the random number.
For instance, if we wish to produce a random number between 1 and 6, we would use this function like-
No = rand() % 6 + 1
Syntax:
int rand()
// The return of this function is an integer value between 0 and RAND_MAX.
Program 1:
When we compile and run the program, the result must always be the same sequence of integers, for example, if we are using C or C++ to generate 5 random numbers using the rand() function in a loop.
C Program:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("The following are random numbers: ");
//using for loop
for(int index = 0; index < 5; index++)
//printing random numbers
printf(" %d ", rand());
return 0;
}
C++ Program:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
printf("The following are random numbers: ");
//using for loop
for(int index = 0; index < 5; index++)
//printing random numbers
cout << rand() << " ";
return 0;
}
Note: Each time this programme is run, the same random number sequence will be generated.
Output 1:
The outcome of running this code for the FIRST time is
The following are random numbers: 41 18467 6334 26500 19169
Output 2:
The outcome of running this code for the nth time is
The following are random numbers: 41 18467 6334 26500 19169
What is srand() ?
The built-in C++ STL function srand() is defined in the <cstdlib> header file. Random number generators are initialised using the srand() function. The pseudo-random integer series can be created using this function as a preliminary step. As a seed for creating a pseudo-random number, the argument is passed. The pseudo number generator should produce different series of outputs whenever a different seed value is used in srand, just like rand ().
Syntax:
int srand(unsigned seed);
Before any calls to rand() or the beginning of the program, the pseudo-random number generator should only be seeded once. It shouldn't be seeded or reseeded frequently each time you want to produce a fresh set of pseudo-random numbers.
The seed is typically set using the output of a call to srand(time(0)). Though time() returns a time_t value that changes each time, the pseudo-random number changes with each program call.
Program 2:
When we compile and run the program, the result must always be the different sequence of integers, for example, if we are using C or C++ to generate 6 random numbers using the rand() and srand() function in a loop.
C Program:
//Every time you execute this programme, a unique series of random integers will be generated.
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
int main(void)
{
//using current time as the seed for the random number generator
srand(time(0));
//using loop
for(int index = 0; index < 6; index++)
//printing random numbers
printf(" %d ", rand());
return 0;
}
C++ Program:
//Every time you execute this programme, a unique series of random integers will be generated.
#include <iostream>
#include <cstdlib>
#include<time.h>
using namespace std;
int main(void)
{
//using current time as the seed for the random number generator
srand(time(0));
//using loop
for(int index = 0; index < 6; index++)
//printing random numbers
cout << rand() << " ";
return 0;
}
Note:
Each time you run this program, a unique random number sequence will be generated.
Output 1:
The outcome of running this code for the 1st time is
27326 13773 5125 17250 32431 14917
Output 2:
The outcome of running this code for the 2nd time is
27735 13836 9922 10509 11033 14238
Output n:
The outcome of running this code for the nth time is
27944 13606 6349 10465 15806 3405
Time Complexity:
The time complexity is: O(n)
What is the relationship between srand() and rand()?
In order for rand to produce "random" numbers, srand() sets the seed. It's as if you had called srand(1) to set the seed to one if you don't call srand before your initial call to rand.
Simply put, srand() sets the seed for the rand() function.