To "generate random numbers from 1 to 6 in C++":
- Use the C++ built-in library stdlib.h in the header.
- Use the rand() function to generate random numbers.
- Use the srand() function to generate a different number at each execution.
- Use the time.h library in the header with the srand() function to get different values at each execution, as the value will change relative to the time, which is a better option compared to just using srand().
- Use For-loop to check that numbers are generated randomly from 1 to 6 at each iteration.
- Check if random numbers are generated upon each execution from 1 to 6.
Code to Generate Random Number
Step 1: Code Editor: Open Your Code Editor
To get started with code writing, you need to open your favorite code editor. I prefer to use the OnlineGDB compiler.
Step 2: Write Code: Start Writing Code in C++
Let's code in the code editor step by step.
Write Necessary Headers
#include <iostream> //this library is used to handle input and output
#include <stdlib.h> //library for random number generator a mandatory library
#include <time.h> //used for generating random value relative to time
using namespace std;
Begin with defining all the libraries that are required throughout the function implementation. I used the stdlib.h library to generate the random numbers. time.h is used for all the time-related functions, and it is required to state the current time so that randomness is ensured. You also have to define other basic libraries including iostream and std to deal with all the input-output related operations in C++.
You can also use cstdlib and ctime library instead of stdlib.h time.h, as it is more specific to C++.
Create diceRoll() Function
int diceRoll()
{
srand(time(0)); //this built-in function ensures that upon each execution a random number is generated
cout<<"Roll a Dice"<<endl;
//loop for generation number from 1 to 6
for(int i=0; i<6; i++)
{
int numbers = (rand() % 6) + 1; // this function ensures that the reminder should be displayed and will be from within the range
cout<<" ";
cout << numbers;
}
cout<<endl;
return 0;
}
Now, moving towards the creation of the diceRoll() function, here, I have defined the function return type int to pass the random values to the main function. The first thing is to initialize the srand() function that ensures that on each execution different values are generated, but for the surety, the better approach is to use the time(0) function from time.h library that saves the current time relative to starting value i.e. seeding value and passes it as an argument to the srand(time(0)) function. It will surely give a sequence of random values upon each execution.
Then, I create a for-loop to iterate the dice faces within the range of 1 to 6. In each iteration, whenever a random number is generated, the number will take modules with 6 to not exceed the range and should start with the number 1 and then store the values in the numbers variable. These sequences of numbers will be returned to the Main function.
Make sure to use modules (%) as it will help to generate the number within your defined range.
Create the Main Function
int main(){
cout <<"The Random numbers Generated from 1 to 6"<<endl;
cout<<diceRoll(); // call the diceroll function
return 0;
}
In the end, the Main function is defined to print the sequence of random numbers by calling the diceRoll() function.
Step 3: Code Compilation: Compile the code of Step 2
After writing the code, you must compile the code so that the results will display. If you are with me, just click the run button in the OnlineGDB compiler to start the compilation.
Step 4: Random Number Generated: Output

Congratulations! You have created a simple diceroll game by using the built-in function. Is not this easy? Yes, that is why the use of built-in libraries makes the problem easy for developers.
Alternative Approach: Using Random Library
Curious, is there any other method to generate random numbers in C++? Yes, In C++ 11, there is one more library introduced that is also used to generate random numbers. As rand() library is the default choice of many developers who have been using C++ for decades, but there are some limitations, as it is bound to some specific range using rand_max. Therefore, the random library overcomes this limitation by providing a vast range of distribution with better quality of randomness.
To "generate random numbers from 1 to 6 using Random library in C++":
- Use the C++ built-in library random in the header.
- Use the random_device defined in the library to get the start number i.e. seed value for generating a random number.
- Use the mt19937 engine to generate a random number using a uniform distribution.
- Use the uniform_int_distribution to generate numbers uniformly and define the range from 1 to 6.
- Return the generated values to the Main function to display the Random number.
Code Demonstration using Random Library
#include <iostream> //this library is used to handle input and output
#include <random> //library is needed to generate a random number
using namespace std;
//the function in which a random number is generated for a simple dice roll game
int dicerolled()
{
random_device rd; //this random_device is used to obtain the seed random system
mt19937 gen(rd()); //this is the distribution which is used for generating number
uniform_int_distribution<int> dist(1,6); //this piece of code defines the distribution range from 1 to 6 using a uniform distribution
return dist(gen);
}
int main(){
cout <<"The Random numbers Generated from 1 to 6"<<endl;
cout<<"This is the results after using Random Library "<<endl;
cout<<dicerolled();
return 0;
}
In the above code of the diceRoll() function, I first create the random_device with the name "rd" that is used to obtain the starting seed to initialize the random system. After that I used the mt19937 algorithm to generate the number by using uniform_int_distribution range from 1 to 6 this will generate the random number and return it to the main function.
random_device is the standard generator defined by the library
There are many other algorithms instead of mt19937 if you want to use other engines for number generator you can go through the reference at the end of the page.
Output: Using Random Library

Q: What is the difference between rand() and srand() function?
A: The rand() stands for a random number whereas the srand() stands for a seeding random number both are the function of the same library that is used to generate random values but the srand() is used for stating the initial value for the sequence and to make sure that different value is generated when the program runs.
Q: Can I use the same library in C language instead of C++?
A: Yes, there is not much difference in both the language only the slight syntax difference otherwise you can use the library in the C program as well.
Q: Can I find the maximum and minimum range of random numbers using the rand() library?
A: Yes, you can find the maximum range by using the function RAND_MAX which provides the maximum value range that is exactly the range for int datatype but for minimum, the range always starts from 0.0.
Wrapping Up
In the end, you got to know how easily you can generate random numbers, whether to use the rand() function or an updated library i.e. random. However, there are some reasons for using each library. The rand() function can used if you are making a small-scale application because of its range limitation but, if you are making a large-scale application where the quality cannot neglect, then random is the best option because the random library provides a robust and flexible way to generate the number. C++ developer needs to understand the use case of each library to create a more robust simulation application. Thanks for reading the guide. Happy Coding!
Reference