Question Problem A: Dice game (20 points) (c++) Suppose we are playing a 2-dice
ID: 3681469 • Letter: Q
Question
Question Problem A: Dice game (20 points) (c++)
Suppose we are playing a 2-dice rolling game with the following rules: Start with 0 points. Roll 7 = add one point and roll again Roll anything else (not 7) = add that many points to the current amount and finish the game The “Roll 7” rules can happen multiple times. For example, if they rolled 7, then 7, then 9... they would get 1+1+9 = 11 points. Figure out the distribution of points for this game by simulating it happening 1,000,000 times then display the results. Do not show the probability of any values you never saw.
Example 1: Chance of rolling 2 is 2.7915%
Chance of rolling 3 is 6.0461%
Chance of rolling 4 is 9.3637%
Chance of rolling 5 is 12.7405%
Chance of rolling 6 is 15.9513%
Chance of rolling 7 is 2.6614%
Chance of rolling 8 is 14.2741%
Chance of rolling 9 is 13.4987%
Chance of rolling 10 is 10.5849%
Chance of rolling 11 is 7.2984%
Chance of rolling 12 is 3.9861%
Chance of rolling 13 is 0.6729%
Chance of rolling 14 is 0.1081%
Chance of rolling 15 is 0.0182%
Chance of rolling 16 is 0.0036%
Chance of rolling 17 is 0.0004%
Chance of rolling 19 is 0.0001%
Explanation / Answer
Program:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
const int MAX = 1000000;
srand(time(NULL));
int rolls[MAX] = {0};
for(int i = 0; i < MAX; i++)
{
int die1 = 1 + rand() % 6;
int die2 = 1 + rand() % 6;
int sum = die1 + die2;
int count = 0;
while(sum == 7)
{
count++;
die1 = 1 + rand() % 6;
die2 = 1 + rand() % 6;
sum = die1 + die2;
}
int index = sum + count;
rolls[index]++;
}
for(int i = 0; i < MAX; i++)
{
if(rolls[i] != 0)
cout << "Chance of rolling " << i << " is " << setprecision(4) << fixed << ((double) rolls[i] / MAX * 100.0) << "%" << endl;
}
return 0;
}
Output:
Chance of rolling 2 is 2.7942%
Chance of rolling 3 is 6.0123%
Chance of rolling 4 is 9.3197%
Chance of rolling 5 is 12.6260%
Chance of rolling 6 is 16.0354%
Chance of rolling 7 is 2.6445%
Chance of rolling 8 is 14.3408%
Chance of rolling 9 is 13.5228%
Chance of rolling 10 is 10.6060%
Chance of rolling 11 is 7.3155%
Chance of rolling 12 is 3.9774%
Chance of rolling 13 is 0.6748%
Chance of rolling 14 is 0.1094%
Chance of rolling 15 is 0.0176%
Chance of rolling 16 is 0.0033%
Chance of rolling 17 is 0.0001%
Chance of rolling 18 is 0.0001%
Chance of rolling 22 is 0.0001%
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.