// RollDice.cc // Program to simulate rolling a die with 6 faces N times. // Output is generated by random number generator and converted to range. // Fraction of times the number 6 appears is calculated. #include #include #include using namespace std; int RollDie(); int main() { int outcome, N=0, count_six=0, count_other=0; float fraction_six, fraction_other; // Initialise random number generator with value of system time. srandom(time(NULL)); // Get user input in correct range. while(N<1 || N>1000) { cout << "Input the number of experiments (1-1000): "; cin >> N; } // Perform N experiments. // Call RollDie() N times and record number of sixes. for(int i=0; i< N; i++) { outcome = RollDie(); cout << outcome << endl; if(outcome==6) count_six++; else count_other++; } //Integer variables must be converted (cast) for correct division fraction_six = static_cast(count_six)/N; fraction_other = static_cast(count_other)/N; // Output results cout << "Proportion of outcomes in which 6 was rolled " << fraction_six << endl; cout << "Proportion of outcomes in which other numbers were rolled " << fraction_other << endl; return 0; } // Function to simulate rolling a single 6-sided die. // Each call will randomly return a different integer between 1 and 6. int RollDie() { int randomNumber, die; randomNumber = random(); die = 1 + randomNumber % 6; return die; }