Generating random numbers is an important step in many practical applications like science, arts, statistics, cryptography, gaming, and many other fields. Let’s see how we can generate random numbers in C++ using the rand() function.

The rand() function

The rand() is a built-in function of C++ that returns a pseudo-random integral number in the range of 0 to RAND_MAX (which is equal to the maximum value the int data type can hold, i.e., 2147483647).

Let’s write our program and call the rand() function three times and see the output:

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy