Web Analytics Made Easy -
StatCounter Monty Hall Game Show Program not working correctly - CodingForum

Announcement

Collapse
No announcement yet.

Monty Hall Game Show Program not working correctly

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Monty Hall Game Show Program not working correctly

    Supposedly, according to various sources, if you switch, the probability is 2/3 instead of 1/3. However, my program keeps outputting basically a 50/50 result.

    Here's the code:

    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    using namespace std;

    int main()
    {
    int choice;
    int prize;
    int notprize;
    int switch_yesorno;
    int finalchoice;
    int win=0;
    int loss=0;

    srand((unsigned)time(0));
    int random_integer0, random_integer1, random_integer2;
    int lowest0=1, highest0=3;
    int lowest1=1, highest1=3;
    int lowest2=1, highest2=2;
    int range0=(highest0-lowest0)+1;
    int range1=(highest1-lowest1)+1;
    int range2=(highest2-lowest2)+1;
    for(int i=1; i<100000; i++)
    {

    random_integer0 = lowest0+int(range0*rand()/(RAND_MAX + 1.0));
    prize=random_integer0;
    // cout<<"The prize is "<<prize<<endl;





    random_integer1 = lowest1+int(range1*rand()/(RAND_MAX + 1.0));;
    choice=random_integer1;
    // cout<<"The choice is "<<choice<<endl;

    /*
    if ((prize==1)&&(choice==1))
    notprize=2;
    else if ((prize==1)&&(choice==2))
    notprize=3;
    else if ((prize==1)&&(choice==3))
    notprize=2;
    else if ((prize==2)&&(choice==1))
    notprize=3;
    else if ((prize==2)&&(choice==2))
    notprize=3;
    else if ((prize==2)&&(choice==3))
    notprize=1;
    else if ((prize==3)&&(choice==1))
    notprize=2;
    else if ((prize==3)&&(choice==2))
    notprize=1;
    else //prize=3 and choice=3
    notprize=1;
    */


    //cout<<"The prize is not "<<notprize<<endl;

    random_integer2 = lowest2+int(range2*rand()/(RAND_MAX + 1.0));
    switch_yesorno=random_integer2;
    //cout<<"Switch is "<<switch_yesorno<<endl;

    if ((switch_yesorno==1)&&(prize==1)&&(choice==1))//notprize=2
    finalchoice=3;
    else if ((switch_yesorno==2)&&(prize==1)&&(choice==1))
    finalchoice=1;
    else if ((switch_yesorno==1)&&(prize==1)&&(choice==2))//notprize=3
    finalchoice=1;
    else if ((switch_yesorno==2)&&(prize==1)&&(choice==2))
    finalchoice=2;
    else if ((switch_yesorno==1)&&(prize==1)&&(choice==3))//notprize=2
    finalchoice=1;
    else if ((switch_yesorno==2)&&(prize==1)&&(choice==3))
    finalchoice=3;
    else if ((switch_yesorno==1)&&(prize==2)&&(choice==1)) //notprize=3
    finalchoice=2;
    else if ((switch_yesorno==2)&&(prize==2)&&(choice==1))
    finalchoice=1;
    else if ((switch_yesorno==1)&&(prize==2)&&(choice==2)) //notprize=3
    finalchoice=1;
    else if ((switch_yesorno==2)&&(prize==2)&&(choice==2))
    finalchoice=2;
    else if ((switch_yesorno==1)&&(prize==2)&&(choice==3))//notprize=1
    finalchoice=2;
    else if ((switch_yesorno==2)&&(prize==2)&&(choice==3))
    finalchoice=3;
    else if ((switch_yesorno==1)&&(prize==3)&&(choice==1))//notprize=2
    finalchoice=3;
    else if ((switch_yesorno==2)&&(prize==3)&&(choice==1))
    finalchoice=1;
    else if ((switch_yesorno==1)&&(prize==3)&&(choice==2))//notprize=1
    finalchoice=3;
    else if ((switch_yesorno==2)&&(prize==3)&&(choice==2))
    finalchoice=2;
    else if ((switch_yesorno==1)&&(prize==3)&&(choice==3))//notprize=1
    finalchoice=2;
    else //switch=2, prize=3, choice=3
    finalchoice=3;

    if (finalchoice==prize)
    win++;
    else
    loss++;




    }

    cout<<"The win total is "<<win<<endl;
    cout<<"The loss total is "<<loss<<endl;

    return 0;
    }

  • #2
    Never mind this thread, I just realized I made a stupid error-By setting the switch equal to random, I'm getting skewed results

    Comment

    Working...
    X