You are a contestant on a game show and have won a shot at the prize.Before you,
ID: 3538961 • Letter: Y
Question
You are a contestant on a game show and have won a shot at the prize.Before you, are three doors. $1 million in cash has randomly been paced behind one door. Behind the other two doors are the consolation prizes of dishwasher detergent.The game show host asks you to select a door, and you randomly pick one. However, before revealing the prize behind your door, the game show host reveals one of the othe doors that contains a consolation prize .At this point, the game show host asks if you would like to stick with your original choice or to switch to the remaining door.
Write a function to simulate the game show problem. Your function should randomly select locations for the prizes, select a door at random chosen by the contestant, and then determine whether the contestant would win or lose by sticking with the original choice or switching to the remaining door.
Write a program so that the game simulates 10,000 times and count the number of times the contestant wins when switching versus staying. If you are the contestant, what choice should you make to optimize your chances of winning the cash, or does it matter?
I've been having a hard time coming up with the algorithm of this code. Could someone help me out please.
Thanks in advance!
Explanation / Answer
#include<iostream>
# include <time.h>
using namespace std;
int irand(int limit) {
/* return a random number between 0 and limit inclusive.
*/
int divisor = RAND_MAX/(limit+1);
int retval;
do {
retval = rand() / divisor;
} while (retval > limit);
return retval;
}
int main()
{
int prize[3];
int ch;
int cs=0,cc=0,lc=0,ls=0,i=0,j=10000;
int val1,val;
srand(time(0));
while(j)
{
for(i=0;i<3;i++)
{
prize[i]=1;
}
prize[irand(2)] = 10;
for(i=0;i<3;i++)
{
if(prize[i]!=10)
prize[i]=1;
}
//cout<<"choose from prizes 1-3<<endl";
//cin>>ch;
ch = irand(2);
val = ch;
for(i=0;i<3;i++)
{
if( prize[i]==1 && val != i)
{
//cout<<"one of the consolation prize is at position "<<i+1<<endl;
break;
}
}
//cout<<"choose from rest of the prizes ";
//cin>>ch;
ch = irand(2);
while(ch==i)
ch = irand(2);
val1 = ch;
if(prize[val]==10)
{//cout<<"Congrats you have stuck to your choice and you have won $1 million";
cs++;
}
else ls++;
if(prize[val1]==10 && val1 != val)
{//cout<<"Congrats your change worked and you have won $1 million";
cc++;
}
else
lc++;
//cout<<"You have won a consolation prize";
j--;
}
cout<<"If u had stuck to your choice "<<cs<<" wins and "<<ls<<" losses "<<endl<<"else if you had changed your choice "<<cc<<" wins and "<<lc<<" losses "<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.