Inbuilt Functions

Other Inbuilt Functions

For achieving the generation of unplanned numbers, C++ offers the utilities. We all are aware that most of the built-in functions are comprised in the header file of C++. Moreover, one such header file is the ‘stdlib.h’. stdlib.h is a standard library that has many inbuilt functions such as EXIT_FAILURE, EXIT_SUCCESS, RAND_MAX, NULL, and many more. With the use of these random functions, we can easily achieve the solution we need. Furthermore, we need not write a brief logic to get out. Instead, we can easily call one of these built-in functions as per our requirements. Similarly, to achieve a random number generation, we get 2 built-in random functions that are randomized and rand.

random functions

stdlib.h

stdlib.h is the header of the general-purpose standard library in the C programming language including the functions comprising the memory allocation, process control, conversions, etc. moreover, it is compatible with C++ and is also known as ‘cstdlib’ in C++. The term “stdlib” stands for “standard library”.

randomize()

This function basically generates a random number each time we run the program. Moreover, the result will be exclusive each time we execute the code. This exclusive output makes us trust more on this function. For example, the first time we run the code, it will start generating the output that will be like 75, 23, 56, 24, 5, 21, 76, 32, 20 and 37. Next time, it will be generating the output that will be: 5, 64, 51, 78, 94, 53, 41, 19, 96, and 52. We will get to see what output it will be given with and without the use of this function, by writing down the code in the next section.

Browse more Topics under Inbuilt Functions

rand()

For generating the numbers from 0 to RAND_MAX-1 we will be using this function. Moreover, here RAND_MAX refers to the maximum possible range of the number. Let’s say that we require generating some random numbers in the range, 0 – 99, then the value of RAND_MAX will become 100. Grounded on the need of the application we are willing to build, the value of RAND_MAX is selected. For example, if it’s a game of dice then the RAND_MAX will be 6. If it’s a game of cards then the RAND_MAX will be 52, etc.

Example:

Now that we have an idea of how the random number generator function actually works, let us write down a simple and basic program. It will be generating a random number and printing its output.

Writing the program step by step for getting more vision into the working.

The initial step will be including the 2 header files that we need for the program. The first one is the h to comprise the input as well as the output functionalities. Moreover, this is the input/output stream header file.

                    

#include 

#include 

using namespace std;

int main( )

{

}

int  number;

int  i;

for(i = 1; i <= 10; i++)

{

}

number = rand() % 100;

cout << number  << "\t";

Output:

                    

83           86           77           15           93           35           86           92           49           21

FAQs on Other Inbuilt Functions and Random Functions

Question 1: How do we generate a random number?

Answer: One way to generate a random number is to use the function rand().

Question 2: How do we generate a random number starting from 0 and ending at 9?

Answer: To start the random number generator, we have to call srand(time(0)); Then, we have to set the integer ‘x’ to a value lying between the low (inclusive) and the high (exclusive): int x = int(floor(rand() / (RAND_MAX + 1.0) * (high-low) + low));

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.