rand() and srand() in C++
Last Updated :
19 May, 2025
The rand()
in C++
is a built-in function that is used to generate a series of random numbers. It will generate random numbers in the range [0, RAND_MAX) where RAND_MAX is a constant whose default value may vary but is granted to be at least 32767.
Syntax
The std::rand() function is defined inside the <cstdlib> and <stdlib.h> header file.
C++
This function does not take any parameters and returns a pseudo-random number in the range of [0, RAND_MAX).
Example of rand()
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
for (int i = 0; i < 3; i++)
// Generate random numbers
cout << rand() << endl;
return 0;
}
Output1804289383
846930886
1681692777
srand()
The random number generated by rand() function is generated using an algorithm that gives a series of non-related numbers by taking 1 as the starting point or seed. Due to this, the random number series will aways be same for different function calls. To resolve this problem, we use srand() function.
The srand() function changes the "seed" or the starting point of the algorithm. A seed is an integer used to initialize the random number generator.
Syntax
C++
where, seed is an integer value for random number generator using rand() function. This function does not return any value.
Example
For the seed, we mostly use the current time in the srand() function to ensure a different sequence of random numbers.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
// Use current time as seed for random generator
srand(time(0));
for (int i = 0; i < 3; i++)
cout << rand() << endl;
return 0;
}
Output582797172
409020522
244217599
Random Numbers in Modern C++
The rand() and srand() functions are inherited by C++ from C language. They use the deterministic algorithms to find the next random number. It means that it generates the same sequence of numbers every time for the same seed.
To provide high quality randomness, C++ 11 introduced <random> library with more sophisticated random number generates like mt19937 engine. This engine also uses deterministic sequence generator but with more sophistication and uniform distribution. When seeded with random_device (which provides true non-deterministic random seed value if OS supports), it is almost guaranteed to be more random than rand().
rand() function is mainly used to generate random numbers in our program which can be used in the following applications:
Similar Reads
Output of C programs | Set 33 (rand() and srand()) You came across how to generate random numbers and use of two C function rand() and srand() from the article rand() and srand() in C\C++.Listed below are some output related multiple choice question on random numbers.1. Return type of rand() function is: a) short b) int c) char d) float Answer: b Ex
4 min read
Random password generator in C In this article, we will discuss how to generate a random password of a given length consists of any characters. Approach: The below-given program involves basic concepts like variables, data types, array, loop, etc.Follow the below steps to solve this problem:Take the length of the password and dec
2 min read
Generate a Random Float Number in C++ Random floating numbers can be generated using 2 methods: Using rand()Using uniform real distribution1. Use of rand() We can generate random integers with the help of the rand() and srand() functions. There are some limitations to using srand() and rand(). To know more about the srand() and rand() f
5 min read
Generate a Random Number between 0 and 1 The Task is to generate a random number between 0 and 1. It is obvious that the number between 0 and 1 will be a floating point number. To generate a random number between 0 and 1, we will make use of the rand() function. The rand() function creates a random number. Approach: Generating a Random Num
5 min read
random header in C++ | Set 3 (Distributions) We have discussed generators in Set 1. We have also discussed three distributions in Set 2. In this post, more distributions are discussed. IV.Rate-based Distributions: poisson_distribution Poisson Distribution exponential_distribution Exponential Distribution gamma_distribution Gamma Distribution w
12 min read
shuffle vs random_shuffle in C++ random_shuffleIt randomly rearranges the elements in range [first, last). The function swaps the value of each element with some other randomly picked element. When provided, the function gen determines which element is picked in every case. Otherwise, the function uses some unspecified source of ra
3 min read