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

C++ Creating a Function that generates a single turn in Monte Carlo/Pig: I\'m ha

ID: 660991 • Letter: C

Question

C++ Creating a Function that generates a single turn in Monte Carlo/Pig:

I'm having a large amount of confusion trying to figure out how to code a certain function.

My Code so far:

int singleTurn(int N)
{
  int roll;
int total;
for (N = 0; N = N; N++)
{
roll = 1 + (rand() % 6);
}
total += roll;
cout << total;
return total;

}

What the function needs to do:

For this assignment you will simulate a given number of hold?at?N turns of a game called Pig, and

report the estimated probabilities of the possible scoring outcomes. You are NOT implementing the

game of Pig, only a single turn of this game and simulating this single turn over and over. The value of

N will be acquired via user input, as will the number of repetitions.

Pig is a folk dice game in which every single turn boils down to two simple rules where a player rolls

or does not roll. Each time a roll of the die occurs, an action is taken based on the die value. For our

simulations, we will implement a specific strategy such that once our minimum turn score is reached

the turn will end (opt to hold).

In summary, a turn looks like:

? roll (or roll again) ? a roll of the die occurs

? 2 ? 6: the number is added to the current turn score; the turn continues

? 1: the player loses all points accumulated in the turn (i.e. scores a 0); turn ends

? hold ? The turn ends. A hold may occur for multiple reasons.

singleTurn

? name: singleTurn

? return type: integer, the score of a single turn

? parameters: one integer representing the hold value for a single turn

Explanation / Answer

double rando (int &ig){ /* * RANDO GENERATES A RANDOM NUMBER RAND, USING A LINEAR * CONGRUENTIAL METHOD. THE RECURSION FORMULA * * IRAND = MOD(IRAND * B + 1, A) * * IS USED WITH B = 31415821 AND A = 100000000. THE LAST * DIGIT FROM THE RANDOM INTEGER IRAND IS CHOPPED OF, AND * THE NUMBER IS SCALED TO A REAL VALUE RAND BETWEEN 0 AND 1, * INCLUDING 0 BUT EXCLUDING 1. * * RETURN VALUE: DELIVERED WITH RANDOM NUMBER BETWEEN 0 AND 1 * IG: RANDOM NUMBER GENERATOR SEED, IS DELIVERED * WITH RANDOM INTEGER * */ const int m = 100000000; const int m1 = 10000; const int mult = 31415821; int irand = abs(ig) % m; // MULTIPLY IRAND BY MULT, BUT TAKE INTO ACCOUNT THAT OVERFLOW // MUST BE DISCARDED, AND DO NOT GENERATE AN ERROR. int irandh = int(irand / m1); int irandl = irand % m1; int multh = int(mult / m1); int multl = mult % m1; irand = ((irandh*multl+irandl*multh) % m1) * m1 + irandl*multl; irand = (irand + 1) % m; // CONVERT IRAND TO A REAL RANDOM NUMBER BETWEEN 0 AND 1. double r = int(irand / 10) * 10.0; r = r / m; if ((r 1.0)) r = 0.0; ig = irand; return r; }