HW5 Monte Carlo Simulation Craps In HW3 you will use the cra

HW#5 Monte Carlo Simulation Craps In HW#3, you will use the craps program from your book (figure 5.10) to create a function that can be iteratively called by your main program. Your main program must read the return values from your craps function and keep track of the win frequency. Your craps function should return 0 for a loss and 1 for a win. Your main function should count the number of attempts and the number of wins. Win percentage is the ratio of wins to tries times 100 The output should be a floating point number so you will need a static cast operator if you count in ints. The process of iteratively calling simulations ofrandom processes is called Monte Carlo after the casinos on the French Riviera. Random processes are processes whose outcomes are unpredictable at the start of the processes. The study of nandom processes arose from calculating the odds of gambling processes and creating games which gave the illusion of fairness while guaranteeing the house would take a percentage over time. In order to ensure that your iterative processes are truly random you must seed your random number generator properly. This is done using the \"srandO time(0)) command. If the seeding is done in the craps function and the craps function is called more than once in any given second the craps function will generate the same sequence repeatedly. This problem is cured by giving the srand command a larger scope and only executing it once in the main function before any calls to the craps function. The final question you must answer is \"What are the true odds of winning at the craps game you have just programmed?\". If you run a limited number ofcycles, say 10, of the game you may win or you may lose. This is the nature of the randomness of the game. To see how random the process is simply run the programs multiple times with the same number of cycles each time. To determine an accurate number you must run the simulation many times. The standard of repeatability for your process will be 10 repeated tests with results that vary no more the .2% from each other The numbers shown in the submission format document are not guaranteed to be the correct answers.

Solution

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>

using namespace std;

enum Status { CONTINUE, WON, LOST};

int rollDice()
{
int die1 = 1 + rand() % 6;
int die2 = 1 + rand() % 6;

int sum = die1 + die2;

// cout << \" player rolled \" << die1 << \" + \" << die2<< \" = \" << sum << endl;

return sum;
}

int craps()
{
   int myPoint;
Status gameStatus;

  
int sumOfDice = rollDice();
switch ( sumOfDice )
{
case 7:
case 11:
gameStatus = WON;
break;
case 2:
case 3:
case 12:
gameStatus = LOST;
break;
default:
gameStatus = CONTINUE;
myPoint = sumOfDice;
// cout << \"point is \" << myPoint << endl;
break;
}
while ( gameStatus == CONTINUE )
{
sumOfDice = rollDice();

if ( sumOfDice == myPoint )
gameStatus = WON;
else
if ( sumOfDice == 7 )
gameStatus = LOST;
}

if ( gameStatus == WON ){
// cout << \"Player wins\" << endl;
return 1;
}
else{
//cout << \"player loses\" << endl;
return 0;
}
return 0;
}

int main()
{
   srand( time( 0 ));

   int wins = 0;
   int a=10;
int games;
cin>>games;
   for ( int i = 0; i < games; i ++ ){
       if(craps())
       wins++;
       if(i+1>=a && (i+1)%a==0){
       a=a*10;
       float flWinPercentage = static_cast<float>(wins) / (i+1) * 100;
       cout<<\"Win percentage is \"<<flWinPercentage<<\" Count = \"<<setw(9)<<i+1<<endl;
       }
   }
   return 0;
}

 HW#5 Monte Carlo Simulation Craps In HW#3, you will use the craps program from your book (figure 5.10) to create a function that can be iteratively called by y
 HW#5 Monte Carlo Simulation Craps In HW#3, you will use the craps program from your book (figure 5.10) to create a function that can be iteratively called by y

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site