A dice roller grants you to roll a constructive dice. It is used to generate a sequence of random number mainly use in gambling games like Backgammon, sic bo or Yahtzee. A computational device uses to create a series of random symbols or numbers with lack of any pattern.


How to simulate a Roller Dice in C++

Here is the source code of the program in C++ to simulate N Roller Dice. This program is successfully compiled, and the output is also given below.

#include <iostream>
#include<stdlib.h>
using namespace std;

int main ()
{
  
int num;
  
cout << "Please Enter The number of Dice: ";
  
cin >> num;
 
cout << "The Dice Values are : ";
  
for (int j = 0; j < num; j++)  
    {
      
cout << (rand () % 6) + 1 << " ";
    
} 
return 0;
}

Result

Result of Dice Simulator

Program Explanation

We have included stdlib.h header file, which contains the rand() function that is used to generate a sequence of random number.

The program starts execution from the main method; we declare a variable num and read the value of num form the user. Moreover, we use for loop which will continue until the value of the j variable becomes greater than num variable. In the body of for loop, we used rand() function to generate a sequence, taking its mod with 6, and sum it into 1.

Program Pseudocode

Start
Declare num
Read num
For j = 0 to num do
Generate sequence with rand() mod 6 + 1
write the sequence
Done
End

Related Articles

Last modified: April 29, 2019