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

Generating random numbers is a common requirement in many programming applications, and C++ provides several ways to generate random numbers within a given range. In this article, we will explore different methods to generate random numbers between 1 and 10 in C++.

Method 1:

Using rand function:

One of the simplest methods to generate a random number between 1 and 10 in C++ is the rand function. This function is defined in the <cstdlib> header file and generates a random integer number within a range of 0 to RANDMAX . The value of RANDMAX is implementation-dependent and can vary from compiler to compiler.

Example:

Let's take an example to generate a random number between 1 and 10 using rand function, we can use the following code:

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 code, we have included the <cstdlib> and <ctime> header files. The srand function is used to initialize the random number generator with the current time as the seed. It ensures that every time the program is run, a new sequence of random numbers is generated.

The rand function is used to generate a random integer between 0 and RAND_MAX . To limit the range between 1 and 10, we take the remainder of this number when divided by 10 and add 1 to it.

Method 2:

Using C++11 random library

The C++11 standard introduced a new library called <random> that provides a better way to generate random numbers. This library provides several random number generation engines and distributions that can generate random numbers with a uniform distribution.

Example:

Let's take an example to generate a random number between 1 and 10 using the <random> library, we can use the following code:

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 code, we have included the <random> header file. The randomdevice class is used to obtain a seed value for the random number generator. The mt19937 class is a random number generation engine that produces random numbers with a uniform distribution. The uniformint_distribution class is used to generate random integers within a given range.

By default, the mt19937 engine uses a seed value of 5489 , which can be changed using the seed method. However, it is recommended to use a random_device to obtain a seed value for better randomness.

The uniformintdistribution class generates random integers with a uniform distribution within a given range. In this code, we have specified the range as 1 to 10 using the constructor.

This method provides better randomness and a uniform distribution of generated numbers compared to the rand function. However, it is slower and more complex to implement.

Method 3:

Using modulo operator with time:

Another method to generate a random number between 1 and 10 is the modulo operator with the current time as a seed value. This method is similar to the first method using rand function, but it uses a more random seed value and provides better randomness.

Example:

Let's take an example to generate a random number between 1 and 10 using the modulo operator with time , we can use the following code:

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, we have used the time function to obtain the current time as a seed value for the srand function. The srand function is used to initialize the random number generator. The rand function generates a random integer between 0 and RAND_MAX , which is then limited to a range between 1 and 10 using the modulo operator and adding 1 to it.

Conclusion:

In conclusion, there are several methods to generate random numbers between 1 and 10 in C++. The choice of method depends on the requirements of the application, such as speed, randomness , and uniformity of generated numbers. While the rand function is the simplest and easiest to implement, it may not provide good randomness and uniformity. The <random> library provides a better way to generate random numbers with a uniform distribution, but it is slower and more complex to implement. The XORShift algorithm provides good randomness and uniformity , but it is more complex to implement and may not be as fast as the rand function.

Input Required

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