Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to simulate a uniform (meaning every number is equally likely to

ID: 3672113 • Letter: W

Question

Write a program to simulate a uniform (meaning every number is equally likely to appear as the next) integer random number. Your program will be a console application which will simulate a 6-sided die. You will “throw” your simulated die N times. The number N should be obtained by prompting the user with the question “How many rolls?”

Also, since you will be using rand() provide a prompt requesting that the user provide a seed number. The prompt can be as simple as "Please enter a seed number:"

Then use the seed provided by the user to seed the random number generator. This will also help you test your program.

We will use 6000 times as an example.

Show the frequency distribution of your numbers when 6,000 tosses are made and the random number is instantiated to generate numbers between 1 and 6 (inclusive). In other words, show how many times the numbers 1,2,3,….and 6 have appeared.

The output displayed on the screen should look something like this:

1 ----- 900

2 ----- 950

    Up to

6 ---- 1011

Meaning that the number one appeared 900 times (your mileage may vary), and so on.

The output should also have a section displaying a histogram.

The histogram should look something like this:

1 xxxxxxxxxxxxxx

2 xxxxxxxxxxxxxx

.

.

.

6 xxxxxxxxxxxxxx

Where the ‘x’ represent some number of occurrence of a particular face. Since you are likely to get 100’s of occurrences of a face you will not be able to use one x for one occurrence. Therefore you will have to scale your display so that ‘x’ represents some number of occurrences and so that the largest number of occurrences will fit on a standard console line of 80 characters.

You might want to find the number with the maximum occurrence and represent that maximum occurrence with 60 “x”’s. This means that should the die side appearing the most comes out 1200 times out of 6000, you would want to represent its count with 60 ‘x’’s. This means that each x, in that particular run would represent 20 occurrences of a side. Thus 1200 occurrences are represented by 20 X’s If, on the other hand, the largest number of occurrences of a particular side had been 1800, then you would need each ‘X’ to represent 30 occurrences so that 1800 occurrences would be represented by 60 X’s.

Basically, you want to normalize your graph in such a way that the largest number of occurrences is represented by 60 X’s.

Make sure that your x’s line up and start on the same column.

Then display the histogram obtained when you roll 2 dice and add up the numbers that come up (2 – 12). As in the first part, you will roll the dice N times.

Create 2 outputs as above. 1 output to display the counts for each sum and one output to display the x’s for each sum

Guidelines.

Use plenty of comments to help others understand your code. Program without comments will incur a 10% penalty.

Note that using Arrays or the vector<> types to hold the histogram counts will make your work much, much easier. Those topics are covered in module 5.

Your code must compile. If your code doesn’t compile you will automatically receive a grade of 0.

Your executable must run. If not you will automatically receive a grade of 0.

If your code doesn’t display the histogram and counts appropriately you will suffer a penalty of at least 50% depending on the cause of the error.

Explanation / Answer

#include <bits/stdc++.h>
using namespace std;

/** function to print output **/
void printOutput(int[], int);

/**
function to simulate n dice roll "times"
and store score in count array
**/
void simulateNDiceRoll(int, int, int[]);

int main() {

// take number of rolls as input
int n;
cout << "How many rolls? ";
cin >> n;

// take seed as input
int seed;
cout << "Please enter seed: ";
cin >> seed;

// initialize seed for random number generator
srand(seed);

// to store net score from dice
int singleDiceCount[6] = {0};

// perform simulation for one dice
simulateNDiceRoll(1, n, singleDiceCount);
cout << " Output for single dice roll: ";
printOutput(singleDiceCount, 6);


// to store net score from 2 dice
int doubleDiceCount[12] = {0};

// perform simulation for two dice
simulateNDiceRoll(2, n, doubleDiceCount);
cout << " Output for double dice roll: ";
printOutput(doubleDiceCount, 12);

return 0;
}

/**
simulate dice roll and fills score in 6*n array
@param nDice number of dice to simulate roll
@param times number of times to roll
@param count[] array in which score to be stored, size should be at least 6 * nDice
**/
void simulateNDiceRoll(int nDice, int times, int count[]) {
// simulate roll
for (int i = 0; i < times; ++i) {
// net score for a single roll
int score = 0;

// calculate random score from each die
// and add it to net score
for (int j = 0; j < nDice; ++j) {
score += (rand() % 6 + 1);
}

// increment this scores count in array
count[score - 1]++;
}
}

/** function to print output **/
void printOutput(int count[], int len) {
// for each print its score
for (int i = 0; i < len; ++i) {
cout << (i + 1) << " ----- " << count[i] << endl;
}

// get the highest count
int highest = -1;
for (int i = 0; i < len; ++i) {
highest = max(highest, count[i]);
}

// calculate how much each x represents
int x = highest / 60;

// print histogram
for (int i = 0; i < len; ++i) {
cout << (i + 1) << " ";

// calculate how much x to print
// if count is not exactly divisible add one extra x
int limit = (count[i] / x) + ((count[i] % x) != 0);

// print 'x'
for (int j = 0; j < limit; ++j) {
cout << 'x';
}
cout << endl;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote