# How to generate random number between 1 and 10 in C++

In this post, we will see how to generate random number between 1 and 10 in C++

## Introduction

Random events happen all around us. Sometimes in web-applications we require random results.

For example: When a dice is thrown we expect a random result every time. If not, we will be stuck getting the same number every time. These will be irrelevant results.

💻 Awesome Tech Resources:
• Looking for ⚒️ tech jobs? Go to our job portal.
• Looking for tech events? Go to tech events 🗓️ Calendar.️

Therefore, it becomes necessary to have a random result generator. In natural environment getting a random even is easy, but on a computer it is not that simple. Everything in a computer is in binary i.e. 0 and 1 / true and false in such condition getting random number is impossible.

C++ programming language comes with an in-built `pseudo-random number generator (PRNG)` along with `rand ()` and `srand ()` functions which are used to generate random numbers.

Pseudo-Random Number Generator (PRNG) is a program that takes an initial/ starting number and converts it into a new number with the help of math operations. This new number is totally different from the initial number.

Let’s understand srand () and rand () functions a little bit in detail.

`Srand ()` : This function takes the initial value which is used by rand () to create random numbers.It’s called only once, to initiate the process of random number |
`Rand ()` : This function is used to generate random numbers. It is called several times until we want to generate random numbers.Provides a sequence of random numbers every time it is called.

## Generate random numbers between 1 to 10, including 1 to 10

Following is the program used to generate random numbers between 1 to 10, including 1 and 10.
In this program we call the srand () function with the system clock, to initiate the process of generating random numbers. And the rand () function is called with module 10 operator to generate the random numbers between 1 to 10.
Program:

Output:

Random numbers generated between 1 and 10:
6 10 3 8 1 1 10 7 5 4

`rand()` function generates random number and when you use `rand()%10`, it will give you last digit of the number. Since we require random number between 1 and 10, we have used `rand()%10+1` to generate random number between 1 and 10 in C++.

As the above program generates random numbers between 1 to 10, remember that the output received every time the program is run will be different.

Also, this program will give 10 outputs all integers some numbers may repeat. If you wish to get a desired number of random numbers between 1 to 10 all you need to do is enter the number of outputs here:
for(int i=0;i<10;i++) (In place of 10 you can put any number.)

Output:

When we need 15 random numbers between 1 to 10.

Random numbers generated between 1 and 10:
4 7 10 8 3 8 7 6 8 3 6 1 2 6 9

## Generate random numbers between 1 to 10, excluding 1 to 10

Following is the program used to generate random numbers between 1 to 10, excludes 1 and 10.
Program:

Output:

Random numbers generated between 1 and 10:
6 6 5 6 4 9 7 7 5 5

## Generate random numbers between 1 to 10 including decimal values

Following is the program used to Random numbers generated between 1 to 10 including decimal values.

Output:
Output:

Random numbers generated between 1 to 10 including decimal values:
1.98767 2.5695 9.31038 8.05644 1.14007 1.55395 9.24143 6.92331 4.13387 8.3827

This is how you can generate random numbers between 1 to 10 with different conditions.

## Uses

• Gambling. For example: A Slot machine.
• Statistical sampeling.
• Computer Simulation.
• Cryptography,
• Completely randome designing.
• And many more areas where unpredictable results are desired.

That’s all about how to generate random number between 1 and 10 in C++.

## Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.