How to Generate Random Value by Dice Roll in C++? Last Updated : 28 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 diceOutput:Dice roll result is: 4Generating Random Values Similar to a Dice RollTo simulate a dice roll, we can use the <random> 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:Define a range (here to 1 to 6 inclusive).Seed an mt19937 object gen with the time-generated seed.Now, create a uniform_int_distribution<> object distrib. This models a random number generator that produces uniformly distributed integers within a specified range. Pass the minimum and maximum values of the range as parameters.Call the distributor by passing the generator as a parameter to get a generate a random number in a range.Finally, print the generated random number.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 <iostream> #include <random> #include <ctime> 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) Comment More infoAdvertise with us Next Article How to generate a vector with random values in C++? S shivanshmahajan876 Follow Improve Article Tags : C++ Programs C++ CPP Examples misc-cpp Practice Tags : CPP Similar Reads How to generate a vector with random values in C++? Generating a vector with random values means creating a vector of n elements and initialize each element some random values. In this article, we will learn different methods to initialize a vector with random values in C++.The recommended method to initialize a vector with random values is by using 3 min read How to Generate Random Number in Range in C++? In C++, we have a <random> header that consists of standard library facilities for random number generation. In this article, we will learn how to generate a random number in range in C++. Example: Input: Range: 1 to 20Output: Random number between 1 and 20 is: 18Generating a Random Number in 2 min read How to Seed a Random Number Generator in C++? In C++, seeding a random number generator is important for generating different sequences of random numbers on each program run. This process consists of initializing the generator with a starting value, known as a seed. This ensures the randomness and unpredictability required for various applicati 2 min read Generate Random Double Numbers in C++ Double is a data type just like a float but double has 2x more precision than float. One bit for the sign, 11 bits for the exponent and 52* bits for the value constitute the 64-bit IEEE 754 double precision Floating Point Number known as "double." Double has 15 decimal digits of precision. In this a 2 min read How to Create a Random Alpha-Numeric String in C++? Creating a random alpha-numeric string in C++ means generating random characters from the set of alphanumeric characters (i.e., âAâ-âZâ, âaâ-âzâ, and â0â-â9â) and appending them to a string. In this article, we will learn how to create a random alpha-numeric string in C++. Example Output:a8shg1laCre 2 min read Program to generate random alphabets Prerequisite : rand() and srand() Given all alphabets in a character array, print a string of random characters of given size.We will use rand() function to print random characters. It returns random integer values. This number is generated by an algorithm that returns a sequence of apparently non-r 4 min read Like