How to Generate Random Value by Dice Roll in C++? (original) (raw)

Last Updated : 23 Jul, 2025

In C++, we can simulate a dice roll by generating random numbers within a specific range. In this article, we will learn how to generate random values by dice roll in C++.

**Example:

**Input: 
Roll a dice

**Output:
Dice roll result is: 4

Generating Random Values Similar to a Dice Roll

To simulate a dice roll, we can use the library in C++. We will generate a random number between 1 and 6 (inclusive), which corresponds to the possible outcomes of a six-sided dice roll.

Approach:

C++ Program to Generate Random Values by Dice Roll

The below program demonstrates how we can simulate a dice roll in C++.

C++ `

// C++ program to simulate a dice roll

#include #include #include using namespace std;

int main() { // Define range int min = 1; int max = 6;

// Initialize a random number generator
mt19937 gen(time(0));
uniform_int_distribution<> distrib(min, max);

// Generate random number in the range [min, max]
int randomValue = distrib(gen);
cout << " Dice roll result is: " << randomValue << endl;

return 0;

}

`

Output

 Dice roll result is: 2

**Time Complexity: O(1)
**Auxiliary Space: O(1)