• Register
632 points
7 1

In today's tutorial we try to find out in our website kodlogs how rand () works in c ++.

what is random number 

Sometimes we need random number in the program  If you are practicing, you have to get an empty file before you can use it. Before using rand (), it is necessary to save this file under the name of randcpp.  Save the file You can also save with the name of kodlogs but it is important to save the name of the program being practiced. This will save our source file. Most of the time during programming when we are creating the program.  A random number is required. A random number is also required to create a game. 

Where are random numbers used 

Rand is used in our computer to generate a random number  For example, if you do not type value, the computer automatically generates a number, which automatically becomes a variable. To use this function  It is important to include the header file in which this function is defined when we start the program. If we generate a random number from 0 to 1, we will see the number 9  That 3,5,8,9,7,2,1,4,6

 Friends, you will be aware that functions play a very important role in programming. The whole definition of rand () is also present in the library.  These are the functions that the user creates within the program according to his needs.

 For example, 

You explain the task of cleaning the office to your new employee in detail on the first day. The next day, all you have to say is  Clean up. Now you don't have to tell the servant everything again, because he knows his job well. Exactly the same situation can happen with your program.  I need 3 places. So instead of writing its code separately each time, its code is written inside a function in a separate place. Then the name of this function is written in all three places in the program and it is used.  Is hair  Similarly, before using the rand function, it is necessary to include the header file in which this function is defined. Let us look at it in detail, put it into practice and try to understand these functions further.

Example:-

#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    for(int i = 0; i<5; i++)
        printf(" %d ", rand());
   
    return 0;
}

 

OUTPUT

452  1688  3433  85


452  85  3433  1688