# rand() in C++

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Topics Covered

## Overview

rand() function in C++ is a built-in function used to generate random numbers in our code. The range of this random number can be varied from [0 to any maximum number], we just need to define the range in code. the rand() function is defined in the header file named <cstdlib>. So, we must include this header file at the beginning of the code for using it. In many programs, we must need to generate some random numbers. For example, they cannot function well in a dice game without random numbers. Here the rand() function in c++ is used to generate random numbers for when the dice are thrown or shuffled. In c++, random numbers can be generated using two functions "rand()" and "srand()" function. In this article, we will see rand() function in detail.

## Header for rand() in C++

In the C++ Language, the required header for the rand function is

## Syntax of rand() in C++

The syntax of the rand() function is as follows:

## Parameters of rand() in C++

The rand() function in C++ has no parameters.

## Return Value of rand() in C++

The rand() function in C++ returns integer value ranging from zero to any random highest number (0 - rand_max).

Input:

Output:

## Exceptions of rand() in C++

If we generate random numbers in a program using the rand() function, then the generated random number will be in the same sequence throughout the program. This is the only case in rand() function otherwise, there are no exceptions seen in rand() function.

## How does rand() in C++ Work?

rand() function in C++ is used to generate random numbers for a given range in our code. For a computer system, it is not possible to generate any random number because it is a machine, and it works on any task based on 0 and 1 so it can not sense randomness. Hence, a thing called PRNG (Pseudo Random Number Generator) that generates a sequence of random numbers is used in C++. This Pseudo-Random Number Generator is capable of sensing randomness in a program. Rand_max is a constant whose value is set to 32767, found in the header file <cstdlib>.

## Examples

Let us see some examples to understand the rand() function in a better way.

Example 1.

In this example, the rand() function is used to generate random numbers in C++. The sequence of random numbers generated will be repeated each time we run the code.

Output:

• If we again run this code for the second-time, the output will be:

Output:

The output will be the same sequence every time we run the program.

Example 2.

In this example, we will see the maximum integer value that a rand() function can return. The "RAND_MAX" range of rand() function ensures that the return value should be atleast 32767.

Output:

## Conclusion

• rand() function in c++ is used to generate random numbers in a code.
• rand function in c++ returns any integer ranging from (o to rand_max), as defined by the user.
• this function is defined in the header file "cstdlib" in C++.
• using rand() function, the sequence of generated random numbers will be the same for a particular range in a code.
• the random numbers are generated by using a Pseudo-Random Number Generator in C++ as a computer cannot sense randomness because it understands only zero and one. (0 and 1)