Introduction on Random Number Generator in C++
Many times in our programming, there arises a situation to generate the numbers randomly. For example dice game, card distribution to players, apps for shuffling the songs, etc. To handle these things we should have some utilities. In C++ we have two utilities to achieve this random number generation. First, we will look at those functions, understand their needs. Later we will use them in our code to achieve the functionality. So let us start with the functions and the library with which it is associated. In this topic, we are going to learn about the Random Number Generator in C++.
How to Generate Random Number?
To achieve the generation of random numbers, C++ provides the utilities. We all know that most of the built-in functions are included in the header file of C++. One such header file is stdlib.h. It’s a standard library having many inbuilt functions like EXIT_FAILURE, EXIT_SUCCESS, RAND_MAX, NULL, etc. Using these functions we can easily get the required solution. We need not write lengthy logic to get the out. Instead, we can simply call one of these built-in functions as per our needs. Same way, to achieve a random number generation, we have two built-in functions. They are randomized and rand. Using both of them in combination we can get our desired result.
Let us see what those functions are basically.
- randomize()– This function is responsible for generating a random number every time you run the program. The result will be unique each time execution of the code. This unique output makes us rely more on this function. For example, the first time when you run the code it will generate the output will be like 75, 23, 56, 24, 5, 21, 76, 32, 20 and 37. Next time it will generate the output as: 5, 64, 51, 78, 94, 53, 41, 19, 96, and 52. We will see how the output will be with and without using this function, by writing code in the next section.
- rand()– To generate the numbers from 0 to RAND_MAX-1 we will use this function. Here RAND_MAX signifies the maximum possible range of the number. Let’s say we need to generate random numbers in the range, 0 to 99, then the value of RAND_MAX will be 100. Based on the need of the application we want to build, the value of RAND_MAX is chosen. For example, if it’s a dice game then the RAND_MAX will be 6. If it’s a card game then RAND_MAX will be 52 etc.
Just keep in mind that both functions are declared in standard library stdlib.h. So don’t forget to include this header file in your code. Now let us see how we can write a simple random number generator program.
Example with Steps
Now that we got an idea of how the random number generator function works, let us write a basic program that will generate a random number and print the output.
Let’s write the program step by step to get more insight into the working.
- The first step will be to include the two header files needed for the program. The first one if the h to include the input and output functionalities. This is the input-output stream header file. The second and important header file is stdlib.h. As discussed earlier this standard library contains the two functions, rand and randomize. So include both of them at the beginning of the code.
#include <iostream>
#include <stdlib.h>
using namespace std;
- Now that we have included the header files, the next step is to write the main function for the execution of the code. To define the main function in the next part.;
- Now we will write a for loop, for printing the values of the randomly generated number. For simplicity purposes, we are printing 10 numbers. So the max number of increments will be 10.
for(i = 1; i <= 10; i++)
{
}
- Inside for loop, we will write the rand function. The below code tells us that any random number generated will be divided by 100 and the remainder is taken. That means the numbers printed will be from 0 to 99 range. If you want higher ranges modulo number will be different. That is instead of 100 we can place, 150, 200, 100, etc.
number = rand() % 100;
- In the last step, we will print the generated number, using cout.
cout << number << "\t";
- The output of the code is as below. Please keep in mind that you may get different output, since its random number generator.
- The problem with the above code is every time we run the code will get the same set of output. To avoid that we have to use the srand() Here we are referring the time at that instance and generating the new values each time we run the program.
Let us look at the complete code with the srand function.
#include <iostream>
#include <stdlib.h>
using namespace std;
int main( )
{
int number;
int i;
srand(time(0));
for(i = 1; i <= 10; i++)
{
number = rand() % 100;
cout << number << "\t";
}
}
Two execution outputs of the code:
First Execution:
Second Execution:
- Below is the code to generate 5 random numbers within 1000. Change the value of the number and see the output generated at each time. Compare the differences of using unsigned and without using.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
int i, number;
time_t nTime;
number = 5;
srand((unsigned) time(&nTime));
printf("Random numbers are: \n");
for( i = 0 ; i < number ; i++ )
{
printf("%d\n", rand() % 1000);
}
return(0);
}
Output:
For 1st iteration
For 2nd iteration
Conclusion – Random Number Generator in approaches.
Recommended Articles
This is a guide to Random Number Generator in C++. Here we discuss How to Generate Random Number along with examples and steps. You may also look at the following article to learn more –