In C++ with explanations:
Create three values, and put a "goat" in two of those, and a prize in one of those. Assign randomly.
Once the prize is loaded, have a contestant randomly generate a door guess of the three.
Display (or rule out) one of the goat options (it can't be the same door as the guess).
Simulate:
1) The contestant stays on the same door guess. Does the contestant win? Track it.
Repeat 10000 times. New locations for prizes, new guesses, new ruling out of goat option, contestant stays. Display the % win rate.
2) The contestant switches. The contestant will never switch to the ruled out/goat option. Does the contestant win? Track it.
Repeat 10000 times. New locations for prizes, new guesses, new ruling out of goat option, contestant stays. Display the % win rate.
Have both scenarios simulated in the same run (the user should only need to run the program once.)
You should observe a 33% win rate for for option #1, and a 67% win rate for option #2.
#include<bits/stdc++.h>
#define n 10000
using namespace std;
void shfl(char door[]){
int val;
char temp;
for(int i=2;i>0;i--){ //generate a random index then swap the
index with current location
val=rand()%(i+1);
temp=door[i];
door[i]=door[val];
door[val]=temp;
}
}
int main(){
char door[3]={'G','G','P'}; //here G is goat and p is
prize
int cnt=0,win=0,user;
srand(time(0));
while(cnt<n){
shfl(door); //used to shuffle the contents of door
user=rand()%3; // randomly assign value to user
if(door[user]=='P') win++; // if P at a give value then increment
win
cnt++;
}
cout<<((win*100)/n)<<endl;
cnt=0;
win=0;
while(cnt<n){
shfl(door); //used to shuffle the contents of door
user=rand()%3;
if(door[user]=='P'){
win++;
}else{
int prev=user; //if user doesn't win then assign another value to
user such that it is not similar to previous value
while(prev==user){
user=rand()%3;
}
if(door[user]=='P') win++;
}
cnt++;
}
cout<<((win*100)/n)<<endl;
return 0;
}
In C++ with explanations: Create three values, and put a "goat" in two of those, and...