rand() in C++

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
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

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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>.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

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)
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more