Skip to content

Instantly share code, notes, and snippets.

@harsh183
Created July 18, 2016 10:34
Show Gist options
  • Save harsh183/49d15c32f1509cb4f35f6ed63b40c596 to your computer and use it in GitHub Desktop.
Save harsh183/49d15c32f1509cb4f35f6ed63b40c596 to your computer and use it in GitHub Desktop.
import java.util.Random;
public class GameShow
{
public static void main()
{
Random r= new Random();
int stayResult=0;
int switchResult=0;
for(int x=0;x<100000;x++)
{
int[] doors={0,0,0};
int car = r.nextInt(3);
doors[car]=1;
int choiceSwitch=0;
int choiceStay=r.nextInt(3);
int goatDoor=0;
int randDoor=doors[goatDoor];
//next sequence is to calculate a random door with a goat in it,except for the one you chose.
while(goatDoor<3)
{
if(goatDoor!=choiceStay && doors[goatDoor]!=1)
{
break;
}
else
{
goatDoor++;
}
}
choiceSwitch=(3-choiceStay-goatDoor);//this is to calculate the remaining door you can switch to
if(doors[choiceStay]==1)
{
stayResult++;
}
if(doors[choiceSwitch]==1)
{
switchResult++;
}
}
System.out.println("Probability of winning by staying: "+(stayResult/100)+"%");
System.out.println("Probability of winning by switching: "+(switchResult/100)+"%");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment