How To Generate Random Number Between 1 To 10 In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / How To Generate Random Number Between 1 To 10 In C++

How To Generate Random Number Between 1 To 10 In C++

BLUF: Mastering How To Generate Random Number Between 1 To 10 In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: How To Generate Random Number Between 1 To 10 In C++

C++ is renowned for its efficiency. Learn how How To Generate Random Number Between 1 To 10 In C++ enables low-level control and high-performance computing in the tutorial below.

Generating random numbers is a frequent necessity in various programming scenarios, and C++ offers multiple techniques for producing random numbers within a specified range. This guide will delve into diverse approaches for generating random numbers ranging from 1 to 10 in C++.

Method 1:

Using rand function:

One of the easiest techniques to produce a random number between 1 and 10 in C++ is through the use of the rand function. This function is declared in the <cstdlib> header file and creates a random integer within the range of 0 to RANDMAX. The specific value of RANDMAX is contingent on the implementation and may differ across various compilers.

Example:

Let's consider an instance where we aim to produce a random number within the range of 1 to 10 by utilizing the rand function. The code snippet for achieving this functionality is as follows:

Example

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(0));

cout<< "Random number between 1 and 10 is: "<<endl;
for(int i=0;i<10;i++)
        cout << (rand() % 10) + 1<<" "; 
    return 0;
}

Output

Output

Random number between 1 and 10 is: 
4 5 7 10 7 5 1 7 10 2

In this script, we've imported the <cstdlib> and <ctime> header files. The srand function is employed to set up the random number generator with the present time as the starting point. This guarantees that each program execution results in a distinct set of random numbers.

The rand function is employed to produce a random integer within the range of 0 to RAND_MAX. In order to restrict the range from 1 to 10, we calculate the modulus of this number by 10 and then add 1 to it.

Method 2:

Using C++11 random library

The C++11 standard unveiled a fresh library known as <random> which offers an improved method for producing random numbers. This library includes multiple random number generation engines and distributions that are capable of producing random numbers following a uniform distribution.

Example:

Let's consider an illustration of generating a random number within the range of 1 to 10 utilizing the <random> library. Below is the code snippet that can be employed for this purpose:

Example

#include <iostream>
#include <random>
using namespace std;
int main()
{
random_device rand;
    mt19937 gen(rand());
uniform_int_distribution<>dis(1, 10);
    int random_number = dis(gen);
cout<< "Random number between 1 and 10 is: " <<random_number<<endl;
    return 0;
}

In this script, we've imported the <random> library. The randomdevice class serves the purpose of fetching a seed value for the random number generator. The mt19937 class acts as a random number generator engine that creates random numbers uniformly. The uniformint_distribution class is employed to produce random integers within a specified range.

By default, the mt19937 engine employs a seed value of 5489 and this value can be modified through the seed method. Nonetheless, it is advised to utilize a random_device to acquire a seed value for improved randomness.

The class uniformintdistribution produces random integers uniformly distributed within a specified range. In this code snippet, we have defined the range from 1 to 10 in the constructor.

This approach offers improved unpredictability and an even spread of produced numbers in contrast to the rand function. Nonetheless, it comes with the trade-off of being slower and requiring a more intricate implementation process.

Method 3:

Using modulo operator with time:

Another technique to produce a random number ranging from 1 to 10 involves leveraging the modulo operator along with the current time as a seed value. This approach bears resemblance to the initial method that utilizes the rand function; however, it incorporates a seed value that is more random, thereby enhancing the quality of randomness.

Example:

Let's consider an illustration of generating a random number within the range of 1 to 10 by employing the modulo operator in conjunction with the time function. Below is the code snippet that demonstrates this process:

Example

#include <iostream>
#include <ctime>
using namespace std;
int main()
{
srand(time(0));
cout<< "Random number between 1 and 10 is: " <<endl;
for(int i=0;i<10;i++)
        cout << (rand() % 10) + 1<<" "; 
    return 0;
}

Output

Output

Random number between 1 and 10 is: 
6 6 3 6 10 10 1 7 6 4

In this code snippet, the time function is employed to fetch the current time as a seed value for initializing the random number generator with the srand function. The srand function is crucial for setting up the random number generator. Subsequently, the rand function is utilized to produce a random integer within the range of 0 to RAND_MAX, then constrained within 1 to 10 by applying the modulo operator and adding 1.

Conclusion:

In summary, there exist multiple techniques for producing random numbers ranging from 1 to 10 in C++. The selection of the approach is contingent upon the specific needs of the application, including factors like speed, randomness, and consistency of the generated numbers. While the rand function stands out as the most straightforward and convenient option, it might not deliver satisfactory levels of randomness and uniformity. The <random> library, on the other hand, offers an improved method for generating random numbers with a uniform distribution, albeit at the cost of increased complexity and slower execution. Alternatively, the XORShift algorithm ensures a high degree of randomness and uniformity, although its implementation is more intricate and it may not match the speed of the rand function.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience