Using function to simulate any number of dice rolls. - C++ Function

C++ examples for Function:Function Creation

Description

Using function to simulate any number of dice rolls.

Demo Code

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int rand_0toN1(int n);
int main()//from ww  w . ja v  a2 s  .c o  m
{
   int n = 0;
   int r = 0;
   srand(time(nullptr)); // Set seed for randomizing.
   cout << "Enter number of dice to roll: ";
   cin >> n;
   for (int i = 1; i <= n; ++i) {
      r = rand_0toN1(6) + 1; // Get a number 1 to 6
      cout << r << " ";      // Print it
   }
   return 0;
}
// Random 0-to-N1 Function.
// Generate a random integer from 0 to N-1, with each integer an equal probability.
int rand_0toN1(int n) {
   return rand() % n;
}

Result


Related Tutorials